开发者

how to overwrite drupal default message when posting comments?

I would like to overwrite a default message for posting a comment.

For example, I would like to display "Your comment has been sent to the site moderator and will remain private." instead of "Your comment has been queued for review by site administra开发者_如何学Pythontors and will be published after approval."

I tried hook insert, but it didn't override the message:

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


Don't use that, use String Overrides to change the message instead. In general, if you want to reword text, don't hack it, override it.

In Drupal 7, you can use settings.php to change it directly: (See 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.';


@Maciej Zgadzaj Your solution works fine as well. I found a useful tutorial on 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");
}


The message is being added to session in comment_form_submit(), AFTER new comment is saved.

So either you alter comment form, add your own submit function after the main one, and remove the message there (which seems to be a better idea, as this way this is going to be done only when a new comment is really posted), or...

Actually, no 'or'. Originally wanted to suggest something like hook_init() as an alternative, but no, you don't want to put it there. ;)


There is question over at Drupal Answers SE where the solution given is to use array_search() to search the actual message array, and then compare the translated strings to identify the key of the string to alter. Then showing how to alter it.

This solution works when implemented as part of a module, while changing the message string in the setting file does not.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜