- Issue created by @rclemings
- π©πͺGermany jurgenhaas Gottmadingen
Looking into the Messenger interface at
\Drupal\Core\Messenger\MessengerInterface
it seems that we can implement the following additional actions:- Get all messages and store them in a token
- Get all messages by type (status/warning/error) and store them in a token
- Delete all messages
- Delete all messages by type (status/warning/error)
That would get us to full support of the Messenger, which doesn't provide more functionality like e.g. deleting individual messages.
- πΊπΈUnited States rclemings
That would work fine for my case. As for deleting individual messages, would it be possible to store them in a token as a list, then delete them from Messenger and just reload the ones you need into Messenger from the token list?
- π©πͺGermany jurgenhaas Gottmadingen
This could have been helpful for Drupal CMS where we needed to remove some specific messages during installation. If we added that to the feature list, this would be a great tool in certain environments.
- πΊπΈUnited States rclemings
That would be great. For the benefit of others in the meantime, I ended up doing something like this in a custom module event subscriber:
function custom_module_form_submit(array $form, \Drupal\Core\Form\FormStateInterface $form_state) { // Can't delete single messages, so loop though them, save the ones we want, then delete all by type, and restore the ones we want. $messages = \Drupal::messenger()->messagesByType('status'); $n = 0; foreach($messages as $message) { // if message starts with "Xxxx Xxxx" and ends with "has been created." do nothing, otherwise save it if (str_starts_with($message, "Xxxx Xxxx") && str_ends_with($message, "has been created.")) { } else { $saved_messages[] = $messages[$n]; } $n = $n+1; } \Drupal::messenger()->deleteByType('status'); foreach($saved_messages as $message) { \Drupal::messenger()->addStatus($message); } }