Messenger.php in Drupal 9
File
core/lib/Drupal/Core/Messenger/Messenger.php
View source
<?php
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;
class Messenger implements MessengerInterface {
protected $flashBag;
protected $killSwitch;
public function __construct(FlashBagInterface $flash_bag, KillSwitch $killSwitch) {
$this->flashBag = $flash_bag;
$this->killSwitch = $killSwitch;
}
public function addError($message, $repeat = FALSE) {
return $this
->addMessage($message, static::TYPE_ERROR, $repeat);
}
public function addMessage($message, $type = self::TYPE_STATUS, $repeat = FALSE) {
if (!$message instanceof Markup && $message instanceof MarkupInterface) {
$message = Markup::create((string) $message);
}
if ($repeat || !in_array($message, $this->flashBag
->peek($type))) {
$this->flashBag
->add($type, $message);
}
$this->killSwitch
->trigger();
return $this;
}
public function addStatus($message, $repeat = FALSE) {
return $this
->addMessage($message, static::TYPE_STATUS, $repeat);
}
public function addWarning($message, $repeat = FALSE) {
return $this
->addMessage($message, static::TYPE_WARNING, $repeat);
}
public function all() {
return $this->flashBag
->peekAll();
}
public function deleteAll() {
return $this->flashBag
->clear();
}
public function deleteByType($type) {
return $this->flashBag
->get($type);
}
public function messagesByType($type) {
return $this->flashBag
->peek($type);
}
}