Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alter messages in Drupal 7

There is a couple of messages in drupal. When there is a php warning, an error message is raised, but a module can also raise messages with drupal_set_message(). The question is: Is there a way to alter these messages? For example to replace every 'a' with 'b' in every message.

Thanks!

like image 839
mimrock Avatar asked Feb 09 '11 08:02

mimrock


2 Answers

While there is no message alter on set, you can change them on display via hook_preprocess_status_messages, see http://api.drupal.org/api/drupal/includes--theme.inc/function/theme/7 on preprocess and http://api.drupal.org/api/drupal/includes--theme.inc/function/theme_status_messages/7 .

Edit: also you can try string overrides check http://api.drupal.org/api/drupal/includes--bootstrap.inc/function/t/7 , in short $conf['locale_custom_strings_en']['some message'] = 'some messbge'; for English, change _en for something else if it's not English.

like image 170
chx Avatar answered Sep 27 '22 16:09

chx


String overrides is the best solution, BUT

  • if the thing you are trying to override in the message is a variable OR
  • you are wanting to replace a string in all messages

then string overrides can not help you and here is a solution.

hook_preprocess_status_messages() passes in $variables but the messages are not in $variables, change them in $_SESSION['messages'].

/**
 * Implements hook_preprocess_status_messages()
 */
function MYMODULE_preprocess_status_messages(&$variables) {
  if (isset($_SESSION['messages']['warning'])) {
    foreach ($_SESSION['messages']['warning'] as $key => $msg) {
      if (strpos($msg, 'some text in the message') !== FALSE) {
        $_SESSION['messages']['warning'][$key] = t(
          'Your new message with a <a href="@link">link</a>.',
          array('@link' => url('admin/something'))
        );
      }
    }
  }
}

Credit to Parvind Sharma where I found part of this solution.

like image 20
Duncanmoo Avatar answered Sep 27 '22 16:09

Duncanmoo