How do I programmatically change a system message? Hey there I want to change or hide a drupal message from the CommentForm class.

In my entity_insert() hook I set a new message. 

I have an idea of how to do it but still no idea of where to do it.

idea:


  whether unset the $_SESSION['messages'] and set it with my message
  after I unset it.
  
  Or 
  
  drupal_get_message($type, TRUE).


problem my message will be set before the one I want to exclude. So if I unset the message array my message will be gone, too.

So my Question is where to put those in myModule?

I also tried to hook in the specific form and add a submit handler but that doesn't work.

/**
 * Implements hook_form_FORM_ID_alter().
 */
function my_module_form_comment_form_alter(&amp;$form, FormStateInterface $form_state) {
  $form['actions']['submit']['#submit'][]  = 'my_module_comment_form_submit';
}

/**
 * Submit handler.
 */
function my_module_comment_form_submit(&amp;$form, FormStateInterface $form_state) {
  $message = $_SESSION['messages'];
}


Ok I've figured it out.
But that solution is pretty ugly in my opinion.

I do this in a hook_preprocess.

/**
 * Implements hook_preprocess_HOOK().
 */
function my_module_preprocess_node(&amp;$variables) {
  $user = Drupal::currentUser();
  $entity_id = $variables['attributes']['data-history-node-id'];
  $entity_type = $variables['elements']['comment']['#entity_type'];
  $bundle = $variables['elements']['comment']['#bundle'];
  $comment_type = FieldConfig::load($entity_type . '.' . $bundle . '.comment');
  $maxCommmentUser = $comment_type-&gt;getThirdPartySetting('my_module', 'edit-limit-per-user', FALSE) + 1;
  $limitUser = $comment_type-&gt;getThirdPartySetting('my_module', 'edit-limit-per-user', FALSE);
  $currentCommentUser = my_module_get_user($entity_id);
  $maxCommentNode = $comment_type-&gt;getThirdPartySetting('my_module', 'edit-limit-per-node', FALSE) + 1;
  $limitNode = $comment_type-&gt;getThirdPartySetting('my_module', 'edit-limit-per-node', FALSE);
  $currentCommentNode = my_module_get_node($entity_id);
  if ($maxCommentNode) {
    if ($maxCommentNode &lt;= $currentCommentNode &amp;&amp; !$user-&gt;hasPermission('bypass comment limit')) {
      drupal_get_messages('status', TRUE);
      drupal_set_message(t('The Node you want to comment has reached its maximum of @maxcommentnode comments', ['@maxcommentnode' =&gt; $limitNode]));

    }
  }
  if ($maxCommmentUser) {
    if ($maxCommmentUser &lt;= $currentCommentUser &amp;&amp; !$user-&gt;hasPermission('bypass comment limit')) {
      drupal_get_messages('status', TRUE);
      drupal_set_message(t('You have commented @maxcomment times, please edit or delete some of your older comments', ['@maxcomment' =&gt; $limitUser]));
    }
  }
}
I was seeking a similar solution and found this great post describing using a hook_preprocess_HOOK(), in this case, hook_preprocess_status_messages() - https://stackoverflow.com/questions/38788080/remove-default-message-in-drupal-8

For my purpose to override a message, I use a simple strpos in the logic:

/**
 * Implements hook_preprocess_HOOK().
 *
 * @param $variables
 */
function mymodule_preprocess_status_messages(&amp;$variables) {
  if(isset($variables['message_list']['status'])){
    $status_messages = $variables['message_list']['status'];
    foreach($status_messages as $delta =&gt; $message) {
      if (strpos((string) $message, 'The message that I am expecting') !== FALSE) {
        $variables['message_list']['status'][$delta] = "Some different message text";
      }
    }
  }
}