Score:3

How to not show the "has been created" message?

kz flag

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_insertand 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.

4uk4 avatar
cn flag
@FreeRadical, I don't know what you mean by red herrings. The topic over there is more general and several years old. It evolved by introducing the then new messenger service and you have to read the entire thread if you want to come to the conclusion you summarized here in the question.
4uk4 avatar
cn flag
I don't know the expression red herring, but it reads like you are not acknowledging the effort of each one participating to advance the topic, which is more general than this one. BTW the first vote to the latest answer was from me when I referenced it in my comment. Late answers often have few votes simply because they are too late to get much attention. But this can change over time when people like us are voting.
Free Radical avatar
kz flag
A "red herring" is an English idiom meaning something that distracts from what is relevant to answering a specific question. It was not my intent to disparage the answers in the linked question - only to say that most answers was not relevant to *this* one. I removed the phrase since you object to it.
Free Radical avatar
kz flag
@NoSssweat Yes, one of the answers there worked. But there was plenty that did not help in my specific use case, including the accepted and most upvoted answers. I've added text to the question where I comment on how well these answers fit my use case.
Score:3
cn flag

If you want to add a custom message in a hook, you can clear the messages and then add your custom message. This is quick and easy, and it can be done in most hooks.

Unfortunately, this won't work with hook_node_insert(), so an alternative is to add a custom submit handler to the node creation form and call the Messenger service statically:

  \Drupal::messenger()->deleteByType('status');
  \Drupal::messenger()->addMessage(t('My new custom message.'));

This will get very messy if you override a lot of messages, but if you just need to change a few, this is the approach I use.

jdhildeb avatar
cn flag
It seems the \Drupal::messenger API does not provide a way to delete a single message. A clumsy workaround that worked for me was to grab all message of a particular type (`$messages = \Drupal::messenger()->all()['status']`), then delete all messages of that type, then add back the ones I didn't want to delete into the messenger using `addMessage()`.
Score:2
kz flag

I've accepted and upvoted Patrick Kenny's answer, since it provides 99% of the solution.

However, I realized that I also needed this worked out:

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.

This was added to the question after Patrick Kenny answered, but for those interested in how this final step was solved:

  1. If certain criteria is met insert an error message \Drupal::messenger()->addError(t('Error'));
  2. Sniff for the error message in the submit handler, and if it is present, delete the unwanted status message.

Voilà!

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.