diff --git a/core/core.services.yml b/core/core.services.yml index c8f74d6f4e..49b27089b5 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -1647,4 +1647,4 @@ services: - { name: event_subscriber } messenger: class: Drupal\Core\Messenger\Messenger - arguments: ['@session.flash_bag'] + arguments: ['@session.flash_bag', '@page_cache_kill_switch'] diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index 4ef9d9e5b9..f843f27754 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -457,11 +457,11 @@ function watchdog_exception($type, Exception $exception, $message = NULL, $varia * @see status-messages.html.twig * @see https://www.drupal.org/node/2774931 * - * @deprecated in Drupal 8.5.0, will be removed before Drupal 9.0.0. + * @deprecated in Drupal 8.5.0 and will be removed before Drupal 9.0.0. * Use \Drupal\Core\Messenger\MessengerInterface::addMessage() instead. */ function drupal_set_message($message = NULL, $type = 'status', $repeat = FALSE) { - @trigger_error('drupal_set_message() is deprecated Drupal 8.5.0, 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); + @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->addMessage($message, $type, $repeat); @@ -494,15 +494,14 @@ function drupal_set_message($message = NULL, $type = 'status', $repeat = FALSE) * @see status-messages.html.twig * @see https://www.drupal.org/node/2774931 * - * @deprecated in Drupal 8.5.0, will be removed before Drupal 9.0.0. + * @deprecated in Drupal 8.5.0 and will be removed before Drupal 9.0.0. * Use \Drupal\Core\Messenger\MessengerInterface::all() or * \Drupal\Core\Messenger\MessengerInterface::messagesByType() instead. */ function drupal_get_messages($type = NULL, $clear_queue = TRUE) { - @trigger_error('drupal_get_message() is deprecated Drupal 8.5.0, will be removed before Drupal 9.0.0. Use \Drupal\Core\Messenger\MessengerInterface::all() or \Drupal\Core\Messenger\MessengerInterface::messagesByType() instead. See https://www.drupal.org/node/2774931', E_USER_DEPRECATED); - /** @var \Drupal\Core\Messenger\MessengerInterface $messenger */ - $messenger = \Drupal::hasService('messenger') ? \Drupal::service('messenger') : NULL; - if ($messenger && ($messages = $messenger->all())) { + @trigger_error('drupal_get_message() is deprecated Drupal 8.5.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Messenger\MessengerInterface::all() or \Drupal\Core\Messenger\MessengerInterface::messagesByType() instead. See https://www.drupal.org/node/2774931', E_USER_DEPRECATED); + $messenger = \Drupal::messenger(); + if ($messages = $messenger->all()) { if ($type) { if ($clear_queue) { $messenger->deleteByType($type); diff --git a/core/lib/Drupal.php b/core/lib/Drupal.php index 5e824d8353..d9f9672bd9 100644 --- a/core/lib/Drupal.php +++ b/core/lib/Drupal.php @@ -6,6 +6,7 @@ */ use Drupal\Core\DependencyInjection\ContainerNotInitializedException; +use Drupal\Core\Messenger\MemoryMessenger; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Url; @@ -761,10 +762,13 @@ public static function time() { * Returns the messenger. * * @return \Drupal\Core\Messenger\MessengerInterface - * The messenger service. + * The messenger. */ public static function messenger() { - return static::getContainer()->get('messenger'); + if (static::hasService('messenger')) { + return static::getContainer()->get('messenger'); + } + return new MemoryMessenger(); } } diff --git a/core/lib/Drupal/Core/Messenger/MemoryMessenger.php b/core/lib/Drupal/Core/Messenger/MemoryMessenger.php new file mode 100644 index 0000000000..a433cab1b0 --- /dev/null +++ b/core/lib/Drupal/Core/Messenger/MemoryMessenger.php @@ -0,0 +1,92 @@ +messages[$type])) { + $this->messages[$type][] = $message; + } + + return $this; + } + + /** + * {@inheritdoc} + */ + public function addStatus($message, $repeat = FALSE) { + return $this->addMessage($message, static::TYPE_STATUS); + } + + /** + * {@inheritdoc} + */ + public function addError($message, $repeat = FALSE) { + return $this->addMessage($message, static::TYPE_ERROR); + } + + /** + * {@inheritdoc} + */ + public function addWarning($message, $repeat = FALSE) { + return $this->addMessage($message, static::TYPE_WARNING); + } + + /** + * {@inheritdoc} + */ + public function all() { + return $this->messages; + } + + /** + * {@inheritdoc} + */ + public function messagesByType($type) { + return $this->messages[$type]; + } + + /** + * {@inheritdoc} + */ + public function deleteAll() { + $messages = $this->messages; + $this->messages = []; + return $messages; + } + + /** + * {@inheritdoc} + */ + public function deleteByType($type) { + $messages = $this->messages[$type]; + $this->messages[$type] = []; + return $messages; + } + +} diff --git a/core/lib/Drupal/Core/Messenger/Messenger.php b/core/lib/Drupal/Core/Messenger/Messenger.php index 454c269142..04ecf9c7bc 100644 --- a/core/lib/Drupal/Core/Messenger/Messenger.php +++ b/core/lib/Drupal/Core/Messenger/Messenger.php @@ -3,6 +3,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; @@ -20,14 +21,24 @@ class Messenger implements MessengerInterface { */ protected $flashBag; + /** + * The kill switch. + * + * @var \Drupal\Core\PageCache\ResponsePolicy\KillSwitch + */ + protected $killSwitch; + /** * Messenger constructor. * * @param \Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface $flash_bag * The flash bag. + * @param \Drupal\Core\PageCache\ResponsePolicy\KillSwitch $killSwitch + * The kill switch. */ - public function __construct(FlashBagInterface $flash_bag) { + public function __construct(FlashBagInterface $flash_bag, KillSwitch $killSwitch) { $this->flashBag = $flash_bag; + $this->killSwitch = $killSwitch; } /** @@ -45,7 +56,7 @@ public function addMessage($message, $type = self::TYPE_STATUS, $repeat = FALSE) } // Mark this page as being uncacheable. - \Drupal::service('page_cache_kill_switch')->trigger(); + $this->killSwitch->trigger(); return $this; } diff --git a/core/tests/Drupal/Tests/Listeners/DeprecationListener.php b/core/tests/Drupal/Tests/Listeners/DeprecationListener.php index bada69ced9..b326c1cd62 100644 --- a/core/tests/Drupal/Tests/Listeners/DeprecationListener.php +++ b/core/tests/Drupal/Tests/Listeners/DeprecationListener.php @@ -111,8 +111,8 @@ public static function getSkippedDeprecations() { 'The Drupal\migrate_drupal\Plugin\migrate\source\d6\i18nVariable is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Instead, use Drupal\migrate_drupal\Plugin\migrate\source\d6\VariableTranslation', 'Implicit cacheability metadata bubbling (onto the global render context) in normalizers is deprecated since Drupal 8.5.0 and will be removed in Drupal 9.0.0. Use the "cacheability" serialization context instead, for explicit cacheability metadata bubbling. See https://www.drupal.org/node/2918937', 'Automatically creating the first item for computed fields is deprecated in Drupal 8.5.x and will be removed before Drupal 9.0.0. Use \Drupal\Core\TypedData\ComputedItemListTrait instead.', - 'drupal_set_message() is deprecated Drupal 8.5.0, will be removed before Drupal 9.0.0. Use \Drupal\Core\Messenger\MessengerInterface::addMessage() instead. See https://www.drupal.org/node/2774931', - 'drupal_get_message() is deprecated Drupal 8.5.0, will be removed before Drupal 9.0.0. Use \Drupal\Core\Messenger\MessengerInterface::all() or \Drupal\Core\Messenger\MessengerInterface::messagesByType() instead. See https://www.drupal.org/node/2774931', + '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', + 'drupal_get_message() is deprecated Drupal 8.5.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Messenger\MessengerInterface::all() or \Drupal\Core\Messenger\MessengerInterface::messagesByType() instead. See https://www.drupal.org/node/2774931', ]; }