function LegacyMessenger::getMessengerService

Returns the Messenger service.

Return value

\Drupal\Core\Messenger\MessengerInterface|null The Messenger service.

5 calls to LegacyMessenger::getMessengerService()
LegacyMessenger::addMessage in core/lib/Drupal/Core/Messenger/LegacyMessenger.php
Adds a new message to the queue.
LegacyMessenger::all in core/lib/Drupal/Core/Messenger/LegacyMessenger.php
Gets all messages.
LegacyMessenger::deleteAll in core/lib/Drupal/Core/Messenger/LegacyMessenger.php
Deletes all messages.
LegacyMessenger::deleteByType in core/lib/Drupal/Core/Messenger/LegacyMessenger.php
Deletes all messages of a certain type.
LegacyMessenger::messagesByType in core/lib/Drupal/Core/Messenger/LegacyMessenger.php
Gets all messages of a certain type.

File

core/lib/Drupal/Core/Messenger/LegacyMessenger.php, line 102

Class

LegacyMessenger
Provides a LegacyMessenger implementation.

Namespace

Drupal\Core\Messenger

Code

protected function getMessengerService() {
    // 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.
        
        /** @var \Drupal\Core\Messenger\MessengerInterface $messenger */
        $messenger = \Drupal::service('messenger');
        // Transfer any messages into the service.
        if (isset(static::$messages)) {
            foreach (static::$messages as $type => $messages) {
                foreach ($messages as $message) {
                    // Force repeat to TRUE since this is merging existing messages to
                    // the Messenger service and would have already checked this prior.
                    $messenger->addMessage($message, $type, TRUE);
                }
            }
            static::$messages = NULL;
        }
        return $messenger;
    }
    // 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 Messenger service is not available (i.e. no container). It is also
    // reasonable to assume that if the container becomes available in a
    // subsequent request, a new instance of this class will be created and
    // this code will never be reached. This is merely for BC purposes.
    if (!isset(static::$messages)) {
        // A "session" was already created, perhaps to simply allow usage of
        // the previous method core used to store messages, use it.
        if (isset($_SESSION)) {
            if (!isset($_SESSION['messages'])) {
                $_SESSION['messages'] = [];
            }
            static::$messages =& $_SESSION['messages'];
        }
        else {
            static::$messages = [];
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.