diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index f843f27754..dea0b8c98e 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -462,8 +462,7 @@ function watchdog_exception($type, Exception $exception, $message = NULL, $varia */ function drupal_set_message($message = NULL, $type = 'status', $repeat = FALSE) { @trigger_error('drupal_set_message() is deprecated Drupal 8.5.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Messenger\MessengerInterface::addMessage() instead. See https://www.drupal.org/node/2774931', E_USER_DEPRECATED); - /* @var \Drupal\Core\Messenger\MessengerInterface $messenger */ - $messenger = \Drupal::service('messenger'); + $messenger = \Drupal::messenger(); $messenger->addMessage($message, $type, $repeat); return $messenger->all(); } diff --git a/core/lib/Drupal/Core/Messenger/BaseMessenger.php b/core/lib/Drupal/Core/Messenger/BaseMessenger.php index f76c295301..c71b587154 100644 --- a/core/lib/Drupal/Core/Messenger/BaseMessenger.php +++ b/core/lib/Drupal/Core/Messenger/BaseMessenger.php @@ -12,6 +12,27 @@ */ abstract class BaseMessenger implements MessengerInterface { + /** + * {@inheritdoc} + */ + public function addMessage($message, $type = self::TYPE_STATUS, $repeat = FALSE) { + $message = $this->convertToMarkup($message); + if ($this->shouldAddMessage($message, $type, $repeat)) { + $this->doAddMessage($message, $type); + } + return $this; + } + + /** + * Add a message in an implementation-specific way. + * + * @param string|\Drupal\Component\Render\MarkupInterface $message + * The message. + * @param string $type + * The message type. + */ + abstract protected function doAddMessage($message, $type); + /** * {@inheritdoc} */ @@ -49,4 +70,23 @@ protected function convertToMarkup($message) { return $message; } + /** + * Returns whether a message should be added or not. + * + * @param string|\Drupal\Component\Render\MarkupInterface $message + * The message. + * @param string $type + * The type. + * @param bool $repeat + * Whether to repeat or not. + * + * @return bool + * TRUE if the message should be added, FALSE otherwise. + */ + protected function shouldAddMessage($message, $type, $repeat) { + // Do not use strict type checking so that equivalent string and + // MarkupInterface objects are detected. + return $repeat || !in_array($message, $this->messagesByType($type)); + } + } diff --git a/core/lib/Drupal/Core/Messenger/ChainedMessenger.php b/core/lib/Drupal/Core/Messenger/ChainedMessenger.php index 21b39b691b..1356dfde26 100644 --- a/core/lib/Drupal/Core/Messenger/ChainedMessenger.php +++ b/core/lib/Drupal/Core/Messenger/ChainedMessenger.php @@ -9,9 +9,11 @@ * container is initialized. When initialised, it will copy over messages and * use that thereafter. * + * You should not use this class directly. Instead use the 'messenger' service. + * * @internal */ -class ChainedMessenger extends BaseMessenger { +class ChainedMessenger implements MessengerInterface { /** * The memory messenger. @@ -25,7 +27,7 @@ class ChainedMessenger extends BaseMessenger { * * @var \Drupal\Core\Messenger\MessengerInterface */ - protected $serviceMessenger; + protected $messengerService; /** * ChainedMessenger constructor. @@ -41,6 +43,27 @@ public function addMessage($message, $type = self::TYPE_STATUS, $repeat = FALSE) return $this->getMessenger()->addMessage($message, $type, $repeat); } + /** + * {@inheritdoc} + */ + public function addStatus($message, $repeat = FALSE) { + return $this->getMessenger()->addStatus($message, $repeat); + } + + /** + * {@inheritdoc} + */ + public function addError($message, $repeat = FALSE) { + return $this->getMessenger()->addError($message, $repeat); + } + + /** + * {@inheritdoc} + */ + public function addWarning($message, $repeat = FALSE) { + return $this->getMessenger()->addWarning($message, $repeat); + } + /** * {@inheritdoc} */ @@ -77,15 +100,15 @@ public function deleteByType($type) { */ protected function getMessenger() { // Shortcut if we have a messenger service. - if (isset($this->serviceMessenger)) { - return $this->serviceMessenger; + if (isset($this->messengerService)) { + return $this->messengerService; } // If the service exists, but we haven't set it yet. if (\Drupal::hasService('messenger')) { - $this->serviceMessenger = \Drupal::service('messenger'); - $this->mergeMessages($this->memoryMessenger, $this->serviceMessenger); - return $this->serviceMessenger; + $this->messengerService = \Drupal::service('messenger'); + $this->mergeMessages($this->memoryMessenger, $this->messengerService); + return $this->messengerService; } // Fallback to memory messenger. diff --git a/core/lib/Drupal/Core/Messenger/MemoryMessenger.php b/core/lib/Drupal/Core/Messenger/MemoryMessenger.php index e82c78e21a..056b0e8a3d 100644 --- a/core/lib/Drupal/Core/Messenger/MemoryMessenger.php +++ b/core/lib/Drupal/Core/Messenger/MemoryMessenger.php @@ -21,15 +21,8 @@ class MemoryMessenger extends BaseMessenger { /** * {@inheritdoc} */ - public function addMessage($message, $type = self::TYPE_STATUS, $repeat = FALSE) { - $message = $this->convertToMarkup($message); - // Do not use strict type checking so that equivalent string and - // MarkupInterface objects are detected. - if ($repeat || !in_array($message, $this->messages[$type])) { - $this->messages[$type][] = $message; - } - - return $this; + protected function doAddMessage($message, $type) { + $this->messages[$type][] = $message; } /** diff --git a/core/lib/Drupal/Core/Messenger/Messenger.php b/core/lib/Drupal/Core/Messenger/Messenger.php index 5dc8fa8bb7..2859070479 100644 --- a/core/lib/Drupal/Core/Messenger/Messenger.php +++ b/core/lib/Drupal/Core/Messenger/Messenger.php @@ -2,9 +2,7 @@ namespace Drupal\Core\Messenger; -use Drupal\Component\Render\MarkupInterface; use Drupal\Core\PageCache\ResponsePolicy\KillSwitch; -use Drupal\Core\Render\Markup; use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; /** @@ -45,12 +43,7 @@ public function __construct(FlashBagInterface $flash_bag, KillSwitch $killSwitch * {@inheritdoc} */ public function addMessage($message, $type = self::TYPE_STATUS, $repeat = FALSE) { - $message = $this->convertToMarkup($message); - // Do not use strict type checking so that equivalent string and - // MarkupInterface objects are detected. - if ($repeat || !in_array($message, $this->flashBag->peek($type))) { - $this->flashBag->add($type, $message); - } + parent::addMessage($message, $type, $repeat); // Mark this page as being uncacheable. $this->killSwitch->trigger(); @@ -58,6 +51,13 @@ public function addMessage($message, $type = self::TYPE_STATUS, $repeat = FALSE) return $this; } + /** + * {@inheritdoc} + */ + protected function doAddMessage($message, $type) { + $this->flashBag->add($type, $message); + } + /** * {@inheritdoc} */