Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 27 additions & 24 deletions includes/message.message.inc
Original file line number Diff line number Diff line change
Expand Up @@ -172,32 +172,35 @@ class Message extends Entity {
if (!empty($arguments)) {
$args = array();
foreach ($arguments as $key => $value) {
if (is_array($value) && !empty($value['callback']) && function_exists($value['callback'])) {
// A replacement via callback function.
$value += array('pass message' => FALSE);
if ($value['pass message']) {
// Pass the message object as-well.
$value['callback arguments'][] = $this;
// Don't bother processing for keys not present in the output
if (strpos($output, $key) !== FALSE) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is the change, right? It's smart!

To keep the diff smaller - and I tend to prefer return early you can

if (strpos($output, $key) === FALSE) {
  // The replacement key is not in the output, so save computation.
  continue;
}

I wonder if there are cases where we would still want to computation. We can add a force_computation (or another better name), in the $options.

(This will require a simpleTest)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback; yes, the change is just a one-line check and comment wrapping the business inside the foreach. I like your approach, I'll incorporate that.

I also share your concern that argument callbacks might be (mis)used in a way that creates needed side effects. Adding an option to force callbacks to be executed anyway would allow any such misuers to update to force legacy behavior.

if (is_array($value) && !empty($value['callback']) && function_exists($value['callback'])) {
// A replacement via callback function.
$value += array('pass message' => FALSE);
if ($value['pass message']) {
// Pass the message object as-well.
$value['callback arguments'][] = $this;
}

$value = call_user_func_array($value['callback'], $value['callback arguments']);
}

$value = call_user_func_array($value['callback'], $value['callback arguments']);
}

switch ($key[0]) {
case '@':
// Escaped only.
$args[$key] = check_plain($value);
break;

case '%':
default:
// Escaped and placeholder.
$args[$key] = drupal_placeholder($value);
break;

case '!':
// Pass-through.
$args[$key] = $value;
switch ($key[0]) {
case '@':
// Escaped only.
$args[$key] = check_plain($value);
break;

case '%':
default:
// Escaped and placeholder.
$args[$key] = drupal_placeholder($value);
break;

case '!':
// Pass-through.
$args[$key] = $value;
}
}
}
$output = strtr($output, $args);
Expand Down