Action to manage Messenger stack

Created on 27 April 2023, almost 2 years ago

Problem/Motivation

At present there is no way for ECA to retrieve/delete/manage messages from the Drupal messenger service. It can only add new messages.

<!--break-->

Use case: I'm working on a model that redirects an anonymous user to a login page after adding a commerce product to the cart, and meanwhile removes the product from the cart. (Anonymous isn't allowed to buy that product, and depending on the details, the logged-in user might not be allowed to buy it either, so it will need to be added again after login.)

I would like to be able to delete the "product added to cart" message before the redirect, so the user won't see it. Instead I have to add a "product removed from cart" message.

Original question from Slack: https://drupal.slack.com/archives/C0287U62CSG/p1682555527091069

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

✨ Feature request
Status

Active

Version

1.1

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States rclemings

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • 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

    Yes, that should be possible.

  • πŸ‡©πŸ‡ͺGermany jurgenhaas Gottmadingen
  • πŸ‡©πŸ‡ͺGermany jurgenhaas Gottmadingen
  • πŸ‡©πŸ‡ͺ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);
      }
    }
    
Production build 0.71.5 2024