3 namespace Drupal\Core\Messenger;
5 use Drupal\Component\Render\MarkupInterface;
6 use Drupal\Core\PageCache\ResponsePolicy\KillSwitch;
7 use Drupal\Core\Render\Markup;
8 use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
11 * The messenger service.
13 class Messenger implements MessengerInterface {
18 * @var \Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface
25 * @var \Drupal\Core\PageCache\ResponsePolicy\KillSwitch
27 protected $killSwitch;
30 * Messenger constructor.
32 * @param \Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface $flash_bag
34 * @param \Drupal\Core\PageCache\ResponsePolicy\KillSwitch $killSwitch
37 public function __construct(FlashBagInterface $flash_bag, KillSwitch $killSwitch) {
38 $this->flashBag = $flash_bag;
39 $this->killSwitch = $killSwitch;
45 public function addError($message, $repeat = FALSE) {
46 return $this->addMessage($message, static::TYPE_ERROR, $repeat);
52 public function addMessage($message, $type = self::TYPE_STATUS, $repeat = FALSE) {
53 if (!($message instanceof Markup) && $message instanceof MarkupInterface) {
54 $message = Markup::create((string) $message);
57 // Do not use strict type checking so that equivalent string and
58 // MarkupInterface objects are detected.
59 if ($repeat || !in_array($message, $this->flashBag->peek($type))) {
60 $this->flashBag->add($type, $message);
63 // Mark this page as being uncacheable.
64 $this->killSwitch->trigger();
72 public function addStatus($message, $repeat = FALSE) {
73 return $this->addMessage($message, static::TYPE_STATUS, $repeat);
79 public function addWarning($message, $repeat = FALSE) {
80 return $this->addMessage($message, static::TYPE_WARNING, $repeat);
86 public function all() {
87 return $this->flashBag->peekAll();
93 public function deleteAll() {
94 return $this->flashBag->clear();
100 public function deleteByType($type) {
101 // Flash bag gets and clears flash messages from the stack.
102 return $this->flashBag->get($type);
108 public function messagesByType($type) {
109 return $this->flashBag->peek($type);