Score:1

Confirmation message on comment submission

in flag

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?

Score:4
us flag

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',
];
in flag
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.
in flag
btw, I copy pasted the above lines to settings.php and cleared cache. still I cannot see the language changed
leymannx avatar
ne flag
@chaydru – I think you need to translate the message now on the interface translation page.
in flag
How to do that?
apaderno avatar
us flag
@chaydru This method is still documented in the [default.settings.php](https://api.drupal.org/api/drupal/sites%21default%21default.settings.php/9.3.x) file for Drupal 9.3.x, so it's the method to change strings used by a Drupal core module.
apaderno avatar
us flag
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._
in flag
Agree. Let me check my settings just once again. First time when I made this change, it didn't reflect
in flag
Thanks @apaderno this solution worked
Score:1
de flag

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'));
}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.