class Messenger

Same name in other branches
  1. 9 core/lib/Drupal/Core/Messenger/Messenger.php \Drupal\Core\Messenger\Messenger
  2. 8.9.x core/lib/Drupal/Core/Messenger/Messenger.php \Drupal\Core\Messenger\Messenger
  3. 10 core/lib/Drupal/Core/Messenger/Messenger.php \Drupal\Core\Messenger\Messenger

The messenger service.

Hierarchy

Expanded class hierarchy of Messenger

36 string references to 'Messenger'
AjaxFormBlock::create in core/modules/system/tests/modules/ajax_forms_test/src/Plugin/Block/AjaxFormBlock.php
AreaDisplayLinkTest::assertNoWarningMessages in core/modules/views/tests/src/Kernel/Handler/AreaDisplayLinkTest.php
Assert no warning messages are shown when all display are equal.
AreaDisplayLinkTest::assertWarningMessages in core/modules/views/tests/src/Kernel/Handler/AreaDisplayLinkTest.php
Assert the warning messages are shown after changing the page_1 display.
BlockListBuilder::createInstance in core/modules/block/src/BlockListBuilder.php
BulkForm::create in core/modules/views/src/Plugin/views/field/BulkForm.php

... See full list

File

core/lib/Drupal/Core/Messenger/Messenger.php, line 13

Namespace

Drupal\Core\Messenger
View source
class Messenger implements MessengerInterface {
    
    /**
     * The flash bag.
     *
     * @var \Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface
     */
    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, KillSwitch $killSwitch) {
        $this->flashBag = $flash_bag;
        $this->killSwitch = $killSwitch;
    }
    
    /**
     * {@inheritdoc}
     */
    public function addError($message, $repeat = FALSE) {
        return $this->addMessage($message, static::TYPE_ERROR, $repeat);
    }
    
    /**
     * {@inheritdoc}
     */
    public function addMessage($message, $type = self::TYPE_STATUS, $repeat = FALSE) {
        if (!$message instanceof Markup && $message instanceof MarkupInterface) {
            $message = Markup::create((string) $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);
        }
        // Mark this page as being uncacheable.
        $this->killSwitch
            ->trigger();
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function addStatus($message, $repeat = FALSE) {
        return $this->addMessage($message, static::TYPE_STATUS, $repeat);
    }
    
    /**
     * {@inheritdoc}
     */
    public function addWarning($message, $repeat = FALSE) {
        return $this->addMessage($message, static::TYPE_WARNING, $repeat);
    }
    
    /**
     * {@inheritdoc}
     */
    public function all() {
        return $this->flashBag
            ->peekAll();
    }
    
    /**
     * {@inheritdoc}
     */
    public function deleteAll() {
        return $this->flashBag
            ->clear();
    }
    
    /**
     * {@inheritdoc}
     */
    public function deleteByType($type) {
        // Flash bag gets and clears flash messages from the stack.
        return $this->flashBag
            ->get($type);
    }
    
    /**
     * {@inheritdoc}
     */
    public function messagesByType($type) {
        return $this->flashBag
            ->peek($type);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
Messenger::$flashBag protected property The flash bag.
Messenger::$killSwitch protected property The kill switch.
Messenger::addError public function Adds a new error message to the queue. Overrides MessengerInterface::addError
Messenger::addMessage public function Adds a new message to the queue. Overrides MessengerInterface::addMessage
Messenger::addStatus public function Adds a new status message to the queue. Overrides MessengerInterface::addStatus
Messenger::addWarning public function Adds a new warning message to the queue. Overrides MessengerInterface::addWarning
Messenger::all public function Gets all messages. Overrides MessengerInterface::all
Messenger::deleteAll public function Deletes all messages. Overrides MessengerInterface::deleteAll
Messenger::deleteByType public function Deletes all messages of a certain type. Overrides MessengerInterface::deleteByType
Messenger::messagesByType public function Gets all messages of a certain type. Overrides MessengerInterface::messagesByType
Messenger::__construct public function Messenger constructor.
MessengerInterface::TYPE_ERROR constant An error.
MessengerInterface::TYPE_STATUS constant A status message.
MessengerInterface::TYPE_WARNING constant A warning.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.