The ultimate goal of this issue is to remove \Drupal\Core\Messenger\LegacyMessenger entirely (which is deprecated and scheduled for removal prior to 9.0.0) and solely rely on the Messenger service.

If possible, 3rd party code should use the Messenger service or find alternative solutions that don't involve using this deprecated class.

If there is any 3rd party code that provides a legitimate use case for keeping this around, we may have to re-think how messages work (highly unlikely, but currently a complete unknown).

This is partially due to the fact that the drupal_set_message() and drupal_get_messages() proceedural functions have been around for over 14 years and usage of these functions was heavy throughout core, contrib and 3rd party code alike.

Thus, this issue also serves as a way to track usage of this deprecated class.

Use cases:

  • Drush - Used when creating a new site and the container is not yet initialized. Unclear if this is a hard dependency or its code can be fixed, needs research
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

markcarver created an issue. See original summary.

markhalliwell’s picture

Title: Usage tracking of \Drupal\Core\Messenger\LegacyMessenger » Remove \Drupal\Core\Messenger\LegacyMessenger
Issue summary: View changes
Status: Active » Postponed
Parent issue: #2760167: Add \Drupal\Core\Messenger\Messenger »
martin107’s picture

Here is a 8.5.x change record

drupal_get_message() and drupal_set_message() replaced by Messenger service

https://www.drupal.org/node/2774931

It was committed to 8.5.x today

#2760167: Add \Drupal\Core\Messenger\Messenger

Version: 9.x-dev » 9.0.x-dev

The 9.0.x branch will open for development soon, and the placeholder 9.x branch should no longer be used. Only issues that require a new major version should be filed against 9.0.x (for example, removing deprecated code or updating dependency major versions). New developments and disruptive changes that are allowed in a minor version should be filed against 8.9.x, and significant new features will be moved to 9.1.x at committer discretion. For more information see the Allowed changes during the Drupal 8 and 9 release cycles and the Drupal 9.0.0 release plan.

JeroenT’s picture

Status: Postponed » Needs review
FileSize
11.77 KB

Patch attached removes the LegacyMessenger, MessengerLegacyTest and replaces the usage of LegacyMessenger in \Drupal::messenger().

JeroenT’s picture

borisson_’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +ContributionWeekend2020, +DUGBE

The patch in #5 seems to do exactly what this issue is asking for.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 5: 2928994-5.patch, failed testing. View results

JeroenT’s picture

Assigned: Unassigned » JeroenT
JeroenT’s picture

Assigned: JeroenT » Unassigned

Hmm, strange that "Module %module has been enabled." is no longer shown after installing a module. I started debugging the test failures but I honestly have no clue what's going on here.

Discussed with @borisson_ and @wimleers. One of the possible reasons is that the ModuleInstaller is using it's own container.

If someone else want's to give this one a go, feel free. #famouslastwords

Berdir’s picture

I suspect what happens is that the container rebuild means we lose the stored messages in there. We might need to do something like \Drupal\Core\Extension\ModuleInstaller::updateKernel() so that we move the stored messages from the old to the new container?

pratik_kamble’s picture

Assigned: Unassigned » pratik_kamble
pratik_kamble’s picture

Assigned: pratik_kamble » Unassigned
andypost’s picture

Berdir’s picture

Status: Needs work » Needs review
FileSize
13.86 KB
2.1 KB

Ok, I think it is even weirder than that.

I made the change that I suggested, directly in DrupalKernel, but that didn't work yet. Then I realized that it only failed on experimental modules. The problem actually is because $this->messenger had been initialized before in ModulesListConfirmForm, so we ended up accessing the service from the old container and added our message there.

As a quickfix, I just unset it now. Rebuilding the container within a running system is a funny business, as usual.

BramDriesen’s picture

Status: Needs review » Reviewed & tested by the community

Funny business indeed :-) But all tests passed so I guess it should be okay. Deprecation deletions look okay.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/lib/Drupal/Core/DrupalKernel.php
@@ -861,6 +861,7 @@ protected function getKernelParameters() {
+    $all_messages = [];
     if (isset($this->container)) {
       // Save the id of the currently logged in user.
       if ($this->container->initialized('current_user')) {
@@ -876,6 +877,8 @@ protected function initializeContainer() {

@@ -876,6 +877,8 @@ protected function initializeContainer() {
         }
         unset($session);
       }
+
+      $all_messages = $this->container->get('messenger')->all();
     }
 
     // If we haven't booted yet but there is a container, then we're asked to
@@ -936,6 +939,13 @@ protected function initializeContainer() {

@@ -936,6 +939,13 @@ protected function initializeContainer() {
       $this->container->get('current_user')->setInitialAccountId($current_user_id);
     }
 
+    // Re-add messages.
+    foreach ($all_messages as $type => $messages) {
+      foreach ($messages as $message) {
+        $this->container->get('messenger')->addMessage($message, $type);
+      }
+    }

This logic makes me think a couple of things.

I think we really should explore having a micro-container that cannot change during a request and is a solid base for Drupal to stand on.

My other thought that is more actionable is that we're storing messages in the session.flash_bag service. So does that mean our session save saves them and if so is this really necessary - and if it is necessary then maybe we're preserving the wrong thing - i.e. should we be preserving just the state of the page cache kill switch or should we be preserving the state of the session.flash_bag service.

Berdir’s picture

I guess we can probably skip that part and only keep the change in the confirm form?

alexpott’s picture

Yep I think that works - at least it does locally. I wonder if we have test coverage of a message surviving a container rebuild.

alexpott’s picture

Status: Needs work » Needs review
FileSize
3.37 KB
15.47 KB

Patch removes the changes to DrupalKernel and adds a test to ensure that messages survive a container rebuild.

jibran’s picture

Issue tags: +Needs manual testing

Can someone please test this manually with drush site:install as well? Not adding the LegacyMessenger first time broke the drush si.

Berdir’s picture

Well, LegacyMessenger has been a BC layer, so it's drush that will need to change if it breaks now? Besides, drush on D9 is broken for me anyway atm.

alexpott’s picture

Issue tags: -Needs manual testing

Tested on Drush 10 and it works fine - both minimal and standard.

Berdir’s picture

Status: Needs review » Reviewed & tested by the community

Nice, setting back to RTBC. I added those changes to try and fix that test, before realizing that the real problem was elsewhere.

  • catch committed 89d0409 on 9.0.x
    Issue #2928994 by Berdir, alexpott, JeroenT, markcarver: Remove \Drupal\...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed 89d0409 and pushed to 9.0.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.