0

I would like to be able to trap when a particular message is sent via drupal_set_message like: "•Article john mitchell article800 has been reverted back to the revision from Tue, 05/28/2013 - 10:03." After the message is sent that matches my criteria then I would like to send an email. I know how to send emails within hooks/php but I need to know how to trap for a particular message so that I know to send this email. Are their any hooks that could use to trap for this occurrence?

1

In Drupal 6, messages are themed for display via...

function theme_status_messages($display = NULL) {
  $output = '';
  foreach (drupal_get_messages($display) as $type => $messages) {
    $output .= "<div class=\"messages $type\">\n";
    if (count($messages) > 1) {
      $output .= " <ul>\n";
      foreach ($messages as $message) {
        $output .= '  <li>'. $message ."</li>\n";
      }
      $output .= " </ul>\n";
    }
    else {
      $output .= $messages[0];
    }
    $output .= "</div>\n";
  }
  return $output;
}

In Drupal 7, they are themed similarly via...

function theme_status_messages($variables) {
  $display = $variables['display'];
  $output = '';

  $status_heading = array(
    'status' => t('Status message'),
    'error' => t('Error message'),
    'warning' => t('Warning message'),
  );
  foreach (drupal_get_messages($display) as $type => $messages) {
    $output .= "<div class=\"messages $type\">\n";
    if (!empty($status_heading[$type])) {
      $output .= '<h2 class="element-invisible">' . $status_heading[$type] . "</h2>\n";
    }
    if (count($messages) > 1) {
      $output .= " <ul>\n";
      foreach ($messages as $message) {
        $output .= '  <li>' . $message . "</li>\n";
      }
      $output .= " </ul>\n";
    }
    else {
      $output .= $messages[0];
    }
    $output .= "</div>\n";
  }
  return $output;
}

so I'd imagine copying the appropriate function into your theme's template.php and naming it YOURTHEME_status_messages(...) along with some suitable regexing or strposing inside would do the trick pretty directly.

  • I don't want to change the message I just want to also send an email when message with a particular title is sent. – John Mitchell May 29 '13 at 15:42
  • I didn't mean to suggest that; rather, to do something like if (strpos('','')!==FALSE) do something like send an email – Jimajamma May 29 '13 at 17:06
  • I copied the above code into template.php and replaced theme with my theme name and I temporarily replaced the returned $output with a hardcoded string 'testing' but it does not change the message that is sent which is telling me that this function is not trapping when messages are ready to be sent. – John Mitchell May 30 '13 at 15:52
  • Correction from my above comment it appears that only the error messages are trapped by the theme_status_messages function. – John Mitchell May 30 '13 at 16:30
  • did you clear your caches/theme registry after adding this to your template.php? because at least here, it processes all of them and themes them via css with $type. – Jimajamma May 31 '13 at 0:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.