diff --git a/core/core.services.yml b/core/core.services.yml index 0319f0e200..c05d67b4fb 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -1662,4 +1662,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/lib/Drupal/Core/Messenger/Messenger.php b/core/lib/Drupal/Core/Messenger/Messenger.php index e0707304ad..25f3473263 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; @@ -21,12 +22,22 @@ class Messenger implements MessengerInterface { protected $flashBag; /** + * The page cache 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 + * (optional) The page cache kill switch. */ - public function __construct(FlashBagInterface $flash_bag) { + public function __construct(FlashBagInterface $flash_bag, KillSwitch $killSwitch) { + $this->killSwitch = $killSwitch; $this->flashBag = $flash_bag; } @@ -43,6 +54,10 @@ public function addMessage($message, $type = self::TYPE_STATUS, $repeat = FALSE) if ($repeat || !in_array($message, $this->flashBag->peek($type))) { $this->flashBag->add($type, $message); } + + // Mark this page as being uncacheable. + $this->killSwitch->trigger(); + return $this; }