【问题标题】:Alter messages in Drupal 7在 Drupal 7 中更改消息
【发布时间】:2011-02-09 08:40:16
【问题描述】:

drupal 中有几条消息。当出现 php 警告时,会引发错误消息,但模块也可以使用 drupal_set_message() 引发消息。问题是:有没有办法改变这些信息?例如,将每条消息中的每个 'a' 替换为 'b'。

谢谢!

【问题讨论】:

    标签: php drupal drupal-7


    【解决方案1】:

    虽然现场没有消息更改,但您可以通过 hook_preprocess_status_messages 在显示屏上更改它们,参见 http://api.drupal.org/api/drupal/includes--theme.inc/function/theme/7 预处理和 http://api.drupal.org/api/drupal/includes--theme.inc/function/theme_status_messages/7

    编辑:您也可以尝试字符串覆盖检查 http://api.drupal.org/api/drupal/includes--bootstrap.inc/function/t/7 ,简而言之 $conf['locale_custom_strings_en']['some message'] = 'some messbge'; 用于英语,如果不是英语则将 _en 更改为其他内容。

    【讨论】:

    • 这并没有解决我的问题。使用这些钩子可以使几乎空的数组可用。我的问题是什么?
    • 请在 drupal.stackexchange 上提出一个新问题,现在属于 Drupal 问题。
    【解决方案2】:

    String overrides 是最好的解决方案,但是

    • 如果您试图在消息中覆盖的内容是变量或
    • 您想要替换所有消息中的字符串

    那么字符串覆盖不能帮助你,这里有一个解决方案。

    hook_preprocess_status_messages() 传入 $variables 但消息不在 $variables 中,请在 $_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'))
            );
          }
        }
      }
    }
    

    感谢Parvind Sharma,我在那里找到了这个解决方案的一部分。

    【讨论】:

      【解决方案3】:

      String Overrides 模块不允许您将字符串中的 A 替换为 B,但它允许您替换整个字符串(drupal 6 和 7) http://drupal.org/project/stringoverrides

      但是,如果您更喜欢使用自己的代码 sn-p,我就是这样做的。

      在 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
      }
      

      然后只需运行 update.php 以进行更改

      【讨论】:

        猜你喜欢
        • 2011-07-29
        • 2014-05-23
        • 1970-01-01
        • 2013-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多