1

When a comment is submitted, no confirmation message is shown and I would like to achieve this. I want to show the following message.

Your comment has been added for review and approval.

I tried this code.

function mymod_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  else if ($form_id == 'comment_comment_form') {
    $form['actions']['submit']['#value'] = "your comment has been added for review and approval"
  }
}

This only edits the submission button text. Is there a way to add a submission confirmation message?

2 Answers 2

4

Drupal already shows a similar message, to the users who has the post comments permission but not the skip comment approval permission.
There is no need to use custom code for that: Don't give the skip comment approval permission to the users whose comments must be approved.

If the purpose is changing the message shown by Drupal in that case (Your comment has been queued for review by site administrators and will be published after approval.), it's enough to change the message Drupal uses, for example adding the following line to the settings.php file.

$settings['locale_custom_strings_en'][''] = [
  'Your comment has been queued for review by site administrators and will be published after approval.' => 'Your comment has been added for review and approval',
];
8
  • Thanks for the help. The above snippet doesn't overwrite the language I still see "Your comment has been added for review and approval". the updated message is not showing.
    – chaydru
    Commented Nov 28, 2021 at 2:50
  • btw, I copy pasted the above lines to settings.php and cleared cache. still I cannot see the language changed
    – chaydru
    Commented Nov 28, 2021 at 2:56
  • 1
    @chaydru – I think you need to translate the message now on the interface translation page.
    – leymannx
    Commented Nov 28, 2021 at 9:20
  • 2
    @chaydru This method is still documented in the default.settings.php file for Drupal 9.3.x, so it's the method to change strings used by a Drupal core module.
    – avpaderno
    Commented Nov 28, 2021 at 17:07
  • 2
    The comment in the default.settings.php file says: To override specific strings on your site with or without enabling the Locale module, add an entry to this list. This functionality allows you to change a small number of your site's default English language interface strings.
    – avpaderno
    Commented Nov 28, 2021 at 17:09
1

You would need to add a custom submit handler, and set your message there.

function ahrq_form_alter(&$form, FormStateInterface $form_state, $form_id) {
    else if($form_id == 'comment_comment_form') {
       $form['actions']['submit']['#submit'][] = 'ahrq_comment_form_submit';
    }
}

function ahrq_comment_form_submit(&$form, FormStateInterface $form_state) {
  \Drupal::messenger()->addStatus->($this->t('Your comment has been added for review and approval'));
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.