Can we Modify Drupal Status Messages?

Fri, May 29, 2009 by Alla

If you've ever wished you had control over Drupal status messages generated by contributed modules and/or ever longed for hook_message_alter, you may be interested in this little trick for changing these messages. First, add this theming function to your template.php file (the function name "phptemplate_" may be replaced with your theme name): function phptemplate_status_messages($display = NULL) {
  $output = '';
  foreach (drupal_get_messages($display) as $type => $messages) {
    if (count($messages) > 1) {
      $list  = '';
      $items = '';
      foreach ($messages as $message) {
        
        modify_message($message);
        if (!empty($message)) {
          $items .= '  <li>'. $message ."</li>\n";
        }
      }
      if (!empty($items)) {
        $list = " <ul>\n";
        $list .= $items;
        $list .= " </ul>\n";
      }
    }
    else {
      $message = $messages[0];
      modify_message($message);
      $list = $message;
    }
    
    if (!empty($list)) {
      $output .= "<div class=\"messages $type\">\n";
      $output .= $list;
      $output .= "</div>\n";
    }
  }
  return $output;
}

Now, all you have to do is to create another function which will be hiding/modifying your status messages. Here is a function with examples of these changes:
/**
* Modify/hide a message from user:
*
* @param string $message
*/
function modify_message(&$message) {
//Add here whatever you need to recognize and modify this message:

  global $user;
    
  // Hide these messages:
  $message = preg_replace('/No posts in this group\./', '', $message);
  $message = preg_replace('/Fetching data from GeoNames failed.*/', '', $message);
  
  //If the administrator has removed _himself_, the message should instead of
  //"Username_ABC is no longer a group administrator." say:
  //"You are no longer an administrator for this group. To regain administrator privileges, please contact an administrator for this group."
  $current_name = $user->name;
  preg_match('/(.*) is no longer a\.*/', $message, $matches_A);
   //check if the current user is the same as the user in the message:
  if ($matches_A[1]) preg_match("/$current_name/", $matches_A[1], $matches_B);
  if ($matches_B[0]) $message = "You are no longer an administrator for this group. To regain administrator privileges, please contact an administrator for this group.";
}
Et, voila.

Michael Barton posted on July 10, 2010 3:44 am

There's actually a module already out there that does something similar, look here:

http://drupal.org/project/messages_alter

Some needed features are listed here:

http://www.michaelbarton.name/2010/07/09/drupal-module-status-messages-a...

dereine posted on May 30, 2009 10:56 pm

Have a look at http://drupal.org/node/127262

this would help to close this problem too

dereine posted on May 30, 2009 10:55 pm

Have a look at http://drupal.org/node/127262

this would help to close this problem too

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <h2>
  • Lines and paragraphs break automatically.

More information about formatting options