Alter messages in Drupal 7

▼魔方 西西 提交于 2019-12-06 02:31:58

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.

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.

The String Overrides module doesn't let you replace A with B in strings, but it lets you replace entire strings (drupal 6 and 7) http://drupal.org/project/stringoverrides

However, if you'd prefer to use your own code snippet this is how i've done it.

in mymodule.install

function mymodule_update_7001() {
$custom_strings = array(
    ''=>array(//context is blank
        'Old string' => '', //blanking the string hides it
        'Another old string.' => 'New String'
    )
);

variable_set("locale_custom_strings_en",$custom_strings);   //note, this is only for english language
}

Then just run update.php for the change to kick in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!