You are here

class LegacyMessenger in Drupal 8

Provides a LegacyMessenger implementation.

This implementation is for handling messages in a backwards compatible way using core's previous $_SESSION storage method.

You should not instantiate a new instance of this class directly. Instead, you should inject the "messenger" service into your own services or use \Drupal::messenger() in procedural functions.

Hierarchy

Expanded class hierarchy of LegacyMessenger

Deprecated

in drupal:8.5.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Messenger\Messenger instead.

See also

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

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

2 files declare their use of LegacyMessenger
Drupal.php in core/lib/Drupal.php
Contains \Drupal.
MessengerLegacyTest.php in core/tests/Drupal/KernelTests/Core/Messenger/MessengerLegacyTest.php

File

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

Namespace

Drupal\Core\Messenger
View source
class LegacyMessenger implements MessengerInterface {

  /**
   * The messages.
   *
   * Note: this property must remain static because it must behave in a
   * persistent manner, similar to $_SESSION['messages']. Creating a new class
   * each time would destroy any previously set messages.
   *
   * @var array
   */
  protected static $messages;

  /**
   * {@inheritdoc}
   */
  public function addError($message, $repeat = FALSE) {
    return $this
      ->addMessage($message, static::TYPE_ERROR, $repeat);
  }

  /**
   * {@inheritdoc}
   */
  public function addMessage($message, $type = self::TYPE_STATUS, $repeat = FALSE) {

    // Proxy to the Messenger service, if it exists.
    if ($messenger = $this
      ->getMessengerService()) {
      return $messenger
        ->addMessage($message, $type, $repeat);
    }
    if (!isset(static::$messages[$type])) {
      static::$messages[$type] = [];
    }
    if (!$message instanceof Markup && $message instanceof MarkupInterface) {
      $message = Markup::create((string) $message);
    }

    // Do not use strict type checking so that equivalent string and
    // MarkupInterface objects are detected.
    if ($repeat || !in_array($message, static::$messages[$type])) {
      static::$messages[$type][] = $message;
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function addStatus($message, $repeat = FALSE) {
    return $this
      ->addMessage($message, static::TYPE_STATUS, $repeat);
  }

  /**
   * {@inheritdoc}
   */
  public function addWarning($message, $repeat = FALSE) {
    return $this
      ->addMessage($message, static::TYPE_WARNING, $repeat);
  }

  /**
   * {@inheritdoc}
   */
  public function all() {

    // Proxy to the Messenger service, if it exists.
    if ($messenger = $this
      ->getMessengerService()) {
      return $messenger
        ->all();
    }
    return static::$messages;
  }

  /**
   * Returns the Messenger service.
   *
   * @return \Drupal\Core\Messenger\MessengerInterface|null
   *   The Messenger service.
   */
  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 = [];
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function messagesByType($type) {

    // Proxy to the Messenger service, if it exists.
    if ($messenger = $this
      ->getMessengerService()) {
      return $messenger
        ->messagesByType($type);
    }
    return static::$messages[$type];
  }

  /**
   * {@inheritdoc}
   */
  public function deleteAll() {

    // Proxy to the Messenger service, if it exists.
    if ($messenger = $this
      ->getMessengerService()) {
      return $messenger
        ->deleteAll();
    }
    $messages = static::$messages;
    static::$messages = NULL;
    return $messages;
  }

  /**
   * {@inheritdoc}
   */
  public function deleteByType($type) {

    // Proxy to the Messenger service, if it exists.
    if ($messenger = $this
      ->getMessengerService()) {
      return $messenger
        ->deleteByType($type);
    }
    $messages = static::$messages[$type];
    unset(static::$messages[$type]);
    return $messages;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LegacyMessenger::$messages protected static property The messages.
LegacyMessenger::addError public function Adds a new error message to the queue. Overrides MessengerInterface::addError
LegacyMessenger::addMessage public function Adds a new message to the queue. Overrides MessengerInterface::addMessage
LegacyMessenger::addStatus public function Adds a new status message to the queue. Overrides MessengerInterface::addStatus
LegacyMessenger::addWarning public function Adds a new warning message to the queue. Overrides MessengerInterface::addWarning
LegacyMessenger::all public function Gets all messages. Overrides MessengerInterface::all
LegacyMessenger::deleteAll public function Deletes all messages. Overrides MessengerInterface::deleteAll
LegacyMessenger::deleteByType public function Deletes all messages of a certain type. Overrides MessengerInterface::deleteByType
LegacyMessenger::getMessengerService protected function Returns the Messenger service.
LegacyMessenger::messagesByType public function Gets all messages of a certain type. Overrides MessengerInterface::messagesByType
MessengerInterface::TYPE_ERROR constant An error.
MessengerInterface::TYPE_STATUS constant A status message.
MessengerInterface::TYPE_WARNING constant A warning.