12

I want to change or hide a message shown from the CommentForm class.

In my entity_insert() hook I either unset $_SESSION['messages'] and add a new message or call drupal_get_message($type, TRUE).

The problem is that the new message will be set before the one I want to change; if I unset the message array, the new message will be gone too.

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

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

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

How can I programmatically change a system message?

3
  • Did you try unsetting only $_SESSION['messages']['status'] also?
    – Codium
    Aug 25, 2016 at 8:23
  • Yes my problem was that in my hook the fully message does not exist yet so I have to get it at a later point to check on it an manipulate it.
    – DiDebru
    Aug 25, 2016 at 8:27
  • so maybe set in using different type?
    – Codium
    Aug 25, 2016 at 8:49

7 Answers 7

7

System messages are stored in a Symfony Flashbag, so there is no need to hack session variables, which can change from version to version. In Drupal 8.5 a new Messenger service was introduced to encapsulate the symfony flash bag and you can delete messages with deleteAll():

\Drupal::messenger()->deleteAll();
5
  • 1
    Ah you mean first delete with the mentioned method and then add your own with \Drupal::messenger()->addMessage()?
    – leymannx
    Dec 2, 2019 at 13:00
  • 1
    This is great for adding a new message, but deleteAll() will not remove generic node create messages if placed in hook_entity_insert.
    – jhchnc
    Aug 10, 2021 at 17:51
  • @jhchnc Trying to do this in Drupal 9.3.13, and has found the same thing as jhchnc. This does not remove generic node create messages if placed in hook_entity_insert. May 22, 2022 at 8:54
  • @FreeRadical, my answer was responding to the general topic in the title and the attempts to hack session variables. The timing is a different issue. You can't delete messages which will be set in the future. See this great answer how to add a submit handler after ::save setting the message.
    – 4uk4
    May 22, 2022 at 9:18
  • do a drupal_register_shutdown_function in hook_entity_insert and nuke from the shutdown function... Aug 1, 2022 at 1:46
14

I was seeking a similar solution. I found How can I remove a message shown from a different module? which show how to achieve it using hook_preprocess_status_messages().

/**
 * Implements hook_preprocess_HOOK().
 *
 * @param $variables
 */
function mymodule_preprocess_status_messages(&$variables) {
  if(isset($variables['message_list']['status'])){
    $status_messages = $variables['message_list']['status'];
    foreach($status_messages as $delta => $message) {
      if (strpos((string) $message, 'The message that I am expecting') !== FALSE) {
        $variables['message_list']['status'][$delta] = "Some different message text";
      }
    }
  }
}
1
  • 1
    This helped in my case since I wanted to alter the message and not create a new one. Jan 13, 2020 at 7:19
2

I think the answer might be not to use hook_entity_insert(). By that time, it is too late: the content has already been created, so the system will create the message accordingly.

As the hook_entity_insert documentation says: hook_entity_insert responds to the creation of a new entity.

Maybe try another hook from the Entity API, one that does not cause the unwanted message to be generated.

2

Drupal 8 delete message:

/**
 * Deletes status message after user registration.
 */
function YOURMODULE_user_register_form_submit($form, &$form_state) {
  $messenger = \Drupal::messenger();
  $messenger->deleteByType('status');
}

/**
 * Implements hook_form_alter().
 */
function YOURMODULE_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  switch ($form_id) {
    case 'user_register_form':
      $form['actions']['submit']['#submit'][] = 'forms_opm_user_register_form_submit';
      break;
  }
}
1
1

Assuming the messages in question are form_errors from validation of the form:

Rather than altering the messages after the fact you could override/replace the #validate function(s) that produce the errors in the first place (from hook_form_alter) and provide your own custom errors.

2
  • I tried that too unfortunately this message still shows up :-/
    – DiDebru
    Aug 25, 2016 at 9:32
  • The message is a status message not an error message.
    – DiDebru
    Aug 25, 2016 at 12:42
0
/**
 * Implements hook_preprocess_HOOK().
 */
function comment_limit_preprocess_node(&$variables) {
  if (in_array('node-max', $_SESSION['messages']['status']) && in_array('user-max', $_SESSION['messages']['status'])) {
    drupal_get_messages('status', TRUE);
    drupal_set_message(t('The comment limit for this node has been reached!'));
    drupal_set_message(t('Your comment limit has been reached!'));
  }
  elseif (in_array('node-max', $_SESSION['messages']['status'])) {
    drupal_get_messages('status', TRUE);
    drupal_set_message(t('The comment limit for this node has been reached!'));
  }
  elseif (in_array('user-max', $_SESSION['messages']['status'])) {
    drupal_get_messages('status', TRUE);
    drupal_set_message(t('Your comment limit has been reached!'));
  }
}

This looks a little prettier and it works.

0

You can do unset($SESSION['_symfony_flashes']['status'][0]). In my case, unset($_SESSION['messages']['status']) didn't work. Then I was able to do \Drupal::messenger()->addMessage(t('My message here')).

Hope this helps someone.

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.