【问题标题】:how to overwrite drupal default message when posting comments?发表评论时如何覆盖drupal默认消息?
【发布时间】:2011-06-29 13:53:42
【问题描述】:

我想覆盖默认消息以发表评论。

例如,我想显示“您的评论已发送给网站管理员并将保持私密。”而不是“您的评论已排队等待网站管理员审核审核通过后发布。"

我尝试了钩子插入,但它没有覆盖消息:

function custom_comment_insert($comment) {
    //drupal_get_messages(null, true);
    unset($_SESSION['messages']);
    drupal_set_message(t('override like this.'));
}

【问题讨论】:

    标签: drupal drupal-modules drupal-7


    【解决方案1】:

    不要使用它,而是使用String Overrides 来更改消息。一般来说,如果你想改写文本,不要修改它,覆盖它。

    在 Drupal 7 中,您可以使用settings.php 直接更改它:(参见http://preprocess.me/drupal-override-strings-in-settingsphp

    $conf['locale_custom_strings_en']['Your comment has been queued for review by site administrators and will be published after approval.'] = 'Your comment has been sent to the site moderator and will remain private.';
    

    【讨论】:

    • 非常感谢。字符串覆盖模块对我有用。
    • String overridessettings.php 如果想以编程方式执行此操作(例如在自定义模块中),则不起作用。
    【解决方案2】:

    @Maciej Zgadzaj 您的解决方案也可以正常工作。 我在 hook_form_alter http://bit.ly/12u09O找到了一个有用的教程

    function private_comments_form_alter(&$form, $form_state, $form_id) {
    switch($form_id) {
            case 'comment_node_proposed_rules_form':
                unset($form['field_comment_public']);
            $form['#submit'][] = 'private_comments_comments_form_submit';
            //$form['#submit'][]='my_submit_test';
            break;
    }
    }
    function private_comments_comments_form_submit($form, &$form_state){
    unset($_SESSION['messages']);
    drupal_set_message("this is a form test");
    }
    

    【讨论】:

      【解决方案3】:

      消息正在被添加到comment_form_submit() 的会话中,新评论保存后。

      所以要么你改变评论表单,在主表单之后添加你自己的提交函数,然后删除那里的消息(这似乎是一个更好的主意,因为这样只有在真正有新评论时才会这样做已发布),或...

      实际上,没有“或”。最初想建议像hook_init() 这样的替代方案,但不,你不想把它放在那里。 ;)

      【讨论】:

      • 在我修改 htdocs/modules/cmets/comment.module 时,这算不算破解了 drupal 的核心
      • @Vlad Vinnikau:不要更改comment.module。将提交函数复制到您自己的模块,将消息更改为您想要的内容,并使用hook_form_alter 以便调用您的函数而不是默认函数。
      • 不不不,坏主意,不是。只需将您自己的提交添加到原始提交,请勿替换。在您的 form_alter() 中,您可以执行以下操作: $form['#submit'][] = 'your_submit_function_name';
      【解决方案4】:

      Drupal Answers SE 有一个问题,给出的解决方案是使用array_search() 搜索实际的消息数组,然后比较翻译的字符串以确定要更改的字符串的键。然后展示如何改变它。

      此解决方案在作为模块的一部分实现时有效,而在设置文件中更改消息字符串则无效。

      【讨论】:

        猜你喜欢
        • 2014-06-01
        • 1970-01-01
        • 2023-03-31
        • 2020-01-17
        • 2010-12-01
        • 2021-08-17
        • 2019-03-20
        • 2014-04-04
        • 2014-03-05
        相关资源
        最近更新 更多