I want to remove the message that is output in the message area when a new node is created. A typical message may look like this.
"Article My article has been created."
I examine the node in hook_node_insert
, and when certain criteria is met, I want to delete the message or prevent the message from appearing.
I've tried to put the following in hook_node_insert
:
$messages = \Drupal::messenger()->all();
dpm($messages, 'messages');
It outputs an empty array: messages => []
.
This similar question for Drupal 7 How do I change/modify an update content message suggests hook_message_alter()
. I haven't found that, or a replacement, for Drupal 9.
It has been suggested that this is a duplicate of How do I programmatically change a system message?
I don't think it is a duplicate, but that question contains a single answer that is helpful to my use case. Most of the answers are not a good fit for this question, so I believe that my question presents a more focused problem and deserves to stay open.
I went through all the answers. These are my findings as to how well they fit this question:
The accepted one (use \Drupal::messenger()->deleteAll();
) does not work when placed in hook_node_insert
, and no alternative placement is suggested.
The most upvoted one (implement hook_preprocess_status_messages
) does not fire after node creation form has been submitted and has no effect.
To use hack the $SESSION
variable (unset($SESSION['_symfony_flashes']['status'][0]);
) does not work in hook_node_insert
and no other placement is suggested.
The answer from Achraf JEDAY, with 1 upvote prior to mine today (Adding a a custom submit handler to the node creation form) works. Cudos.
This is also the accepted answer (from Patrick Kenny) here.