Chris@14: addMessage($message, static::TYPE_ERROR, $repeat); Chris@14: } Chris@14: Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@14: public function addMessage($message, $type = self::TYPE_STATUS, $repeat = FALSE) { Chris@14: // Proxy to the Messenger service, if it exists. Chris@14: if ($messenger = $this->getMessengerService()) { Chris@14: return $messenger->addMessage($message, $type, $repeat); Chris@14: } Chris@14: Chris@14: if (!isset(static::$messages[$type])) { Chris@14: static::$messages[$type] = []; Chris@14: } Chris@14: Chris@14: if (!($message instanceof Markup) && $message instanceof MarkupInterface) { Chris@14: $message = Markup::create((string) $message); Chris@14: } Chris@14: Chris@14: // Do not use strict type checking so that equivalent string and Chris@14: // MarkupInterface objects are detected. Chris@14: if ($repeat || !in_array($message, static::$messages[$type])) { Chris@14: static::$messages[$type][] = $message; Chris@14: } Chris@14: Chris@14: return $this; Chris@14: } Chris@14: Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@14: public function addStatus($message, $repeat = FALSE) { Chris@14: return $this->addMessage($message, static::TYPE_STATUS, $repeat); Chris@14: } Chris@14: Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@14: public function addWarning($message, $repeat = FALSE) { Chris@14: return $this->addMessage($message, static::TYPE_WARNING, $repeat); Chris@14: } Chris@14: Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@14: public function all() { Chris@14: // Proxy to the Messenger service, if it exists. Chris@14: if ($messenger = $this->getMessengerService()) { Chris@14: return $messenger->all(); Chris@14: } Chris@14: Chris@14: return static::$messages; Chris@14: } Chris@14: Chris@14: /** Chris@14: * Returns the Messenger service. Chris@14: * Chris@14: * @return \Drupal\Core\Messenger\MessengerInterface|null Chris@14: * The Messenger service. Chris@14: */ Chris@14: protected function getMessengerService() { Chris@14: // Use the Messenger service, if it exists. Chris@14: if (\Drupal::hasService('messenger')) { Chris@14: // Note: because the container has the potential to be rebuilt during Chris@14: // requests, this service cannot be directly stored on this class. Chris@14: /** @var \Drupal\Core\Messenger\MessengerInterface $messenger */ Chris@14: $messenger = \Drupal::service('messenger'); Chris@14: Chris@14: // Transfer any messages into the service. Chris@14: if (isset(static::$messages)) { Chris@14: foreach (static::$messages as $type => $messages) { Chris@14: foreach ($messages as $message) { Chris@14: // Force repeat to TRUE since this is merging existing messages to Chris@14: // the Messenger service and would have already checked this prior. Chris@14: $messenger->addMessage($message, $type, TRUE); Chris@14: } Chris@14: } Chris@14: static::$messages = NULL; Chris@14: } Chris@14: Chris@14: return $messenger; Chris@14: } Chris@14: Chris@14: // Otherwise, trigger an error. Chris@14: @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); Chris@14: Chris@14: // Prematurely creating $_SESSION['messages'] in this class' constructor Chris@14: // causes issues when the container attempts to initialize its own session Chris@14: // later down the road. This can only be done after it has been determined Chris@14: // the Messenger service is not available (i.e. no container). It is also Chris@14: // reasonable to assume that if the container becomes available in a Chris@14: // subsequent request, a new instance of this class will be created and Chris@14: // this code will never be reached. This is merely for BC purposes. Chris@14: if (!isset(static::$messages)) { Chris@14: // A "session" was already created, perhaps to simply allow usage of Chris@14: // the previous method core used to store messages, use it. Chris@14: if (isset($_SESSION)) { Chris@14: if (!isset($_SESSION['messages'])) { Chris@14: $_SESSION['messages'] = []; Chris@14: } Chris@14: static::$messages = &$_SESSION['messages']; Chris@14: } Chris@14: // Otherwise, just set an empty array. Chris@14: else { Chris@14: static::$messages = []; Chris@14: } Chris@14: } Chris@14: } Chris@14: Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@14: public function messagesByType($type) { Chris@14: // Proxy to the Messenger service, if it exists. Chris@14: if ($messenger = $this->getMessengerService()) { Chris@14: return $messenger->messagesByType($type); Chris@14: } Chris@14: Chris@14: return static::$messages[$type]; Chris@14: } Chris@14: Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@14: public function deleteAll() { Chris@14: // Proxy to the Messenger service, if it exists. Chris@14: if ($messenger = $this->getMessengerService()) { Chris@14: return $messenger->deleteAll(); Chris@14: } Chris@14: Chris@14: $messages = static::$messages; Chris@14: static::$messages = NULL; Chris@14: return $messages; Chris@14: } Chris@14: Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@14: public function deleteByType($type) { Chris@14: // Proxy to the Messenger service, if it exists. Chris@14: if ($messenger = $this->getMessengerService()) { Chris@17: return $messenger->deleteByType($type); Chris@14: } Chris@14: Chris@14: $messages = static::$messages[$type]; Chris@14: unset(static::$messages[$type]); Chris@14: return $messages; Chris@14: } Chris@14: Chris@14: }