diff --git a/core/lib/Drupal.php b/core/lib/Drupal.php index 0b32dfa47c..c28293ab20 100644 --- a/core/lib/Drupal.php +++ b/core/lib/Drupal.php @@ -766,6 +766,7 @@ public static function time() { */ public static function messenger() { // @todo Replace with service once LegacyMessenger is removed in 9.0.0. + // @see https://www.drupal.org/project/drupal/issues/2928994 return new LegacyMessenger(); } diff --git a/core/lib/Drupal/Core/Messenger/LegacyMessenger.php b/core/lib/Drupal/Core/Messenger/LegacyMessenger.php index 7969222489..856b557fbf 100644 --- a/core/lib/Drupal/Core/Messenger/LegacyMessenger.php +++ b/core/lib/Drupal/Core/Messenger/LegacyMessenger.php @@ -37,19 +37,6 @@ class LegacyMessenger implements MessengerInterface { */ protected $messengerService; - /** - * LegacyMessenger constructor. - * - * @deprecated in Drupal 8.5.0 and will be removed before Drupal 9.0.0. - * Use \Drupal\Core\Messenger\Messenger instead. - */ - public function __construct() { - if (!isset($_SESSION['messages'])) { - $_SESSION['messages'] = []; - } - $this->messages = &$_SESSION['messages']; - } - /** * {@inheritdoc} */ @@ -116,14 +103,14 @@ public function all() { * The Messenger service. */ public function getMessenger() { - // Use the Messenger service, if it exists. Note: because the container has - // the potential to be rebuilt during requests, this service cannot be - // directly stored on this class. + // Use the Messenger service, if it exists. if (\Drupal::hasService('messenger')) { + // Note: because the container has the potential to be rebuilt during + // requests, this service cannot be directly stored on this class. $messenger = \Drupal::service('messenger'); // Transfer any messages into the service. - if ($this->messages) { + if (isset($this->messages)) { foreach ($this->messages as $type => $messages) { foreach ($messages as $message) { $messenger->addMessage($message, $type); @@ -137,6 +124,19 @@ public function getMessenger() { // Otherwise, trigger an error. @trigger_error('Adding or retrieving messages prior to the container being initialized was deprecated in Drupal 8.5.0 and this functionality will be removed before Drupal 9.0.0. Please report this usage at https://www.drupal.org/node/2928994.', E_USER_DEPRECATED); + + // Prematurely creating $_SESSION['messages'] in this class' constructor + // causes issues when the container attempts to initialize its own session + // later down the road. This can only be done after it has been determined + // the normal Messenger service is not available (i.e. no container) which + // is reasonable to assume that a new instance of this class will be + // created once the container becomes available. + if (!isset($_SESSION['messages'])) { + $_SESSION['messages'] = []; + } + + // Reference the previous method core used to store messages. + $this->messages = &$_SESSION['messages']; } /** diff --git a/core/tests/Drupal/KernelTests/Core/Messenger/MessengerTest.php b/core/tests/Drupal/KernelTests/Core/Messenger/MessengerTest.php index c9b9258a6e..4bccb8b5a3 100644 --- a/core/tests/Drupal/KernelTests/Core/Messenger/MessengerTest.php +++ b/core/tests/Drupal/KernelTests/Core/Messenger/MessengerTest.php @@ -2,7 +2,6 @@ namespace Drupal\KernelTests\Core\Messenger; -use Drupal\Core\Messenger\LegacyMessenger; use Drupal\Core\Messenger\Messenger; use Drupal\Core\Messenger\MessengerInterface; use Drupal\KernelTests\KernelTestBase; @@ -37,7 +36,7 @@ public function testMessages() { $messenger->addMessage('Foobar'); $messenger->addError('Foo'); - // Verify that adding more messages by retrieving another instance works. + // Verify that retrieving another instance and adding more messages works. $messenger = \Drupal::messenger(); $messenger->addStatus('Bar'); $messenger->addWarning('Fiz');