4

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 administrators 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.'));
}
Vlad Vinnikov
  • 1,457
  • 3
  • 22
  • 33

4 Answers4

4

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.';
Sacha Chua
  • 581
  • 7
  • 6
4

@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");
}
Vlad Vinnikov
  • 1,457
  • 3
  • 22
  • 33
0

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.

Community
  • 1
  • 1
Free Radical
  • 2,052
  • 1
  • 21
  • 35
0

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. ;)

axel22
  • 32,045
  • 9
  • 125
  • 137
Maciej Zgadzaj
  • 631
  • 5
  • 11
  • Would this count as hacking the core of drupal as I'm modifying htdocs/modules/comments/comment.module – Vlad Vinnikov Jun 29 '11 at 17:14
  • @Vlad Vinnikau: Don't alter `comment.module`. Copy the submit function to your own module, change the message to what you want it to say, and use a `hook_form_alter` so that your function is called instead of the default one. – nmc Jun 29 '11 at 17:19
  • No no no, bad idea, NOT instead. Just add your own submit to the original one, DO NOT replace. In your form_alter() you can just do something like: $form['#submit'][] = 'your_submit_function_name'; – Maciej Zgadzaj Jun 29 '11 at 17:37