Unverified Commit 40856ccd authored by larowlan's avatar larowlan

Issue #2760167 by kim.pepper, markcarver, jibran, dawehner, znerol, Wim Leers,...

Issue #2760167 by kim.pepper, markcarver, jibran, dawehner, znerol, Wim Leers, larowlan, xjm, tim.plunkett: Add \Drupal\Core\Messenger\Messenger
parent 5ea62f1d
......@@ -1646,5 +1646,5 @@ services:
tags:
- { name: event_subscriber }
messenger:
class: Drupal\Core\Messenger\LegacyMessenger
arguments: ['@page_cache_kill_switch']
class: Drupal\Core\Messenger\Messenger
arguments: ['@session.flash_bag', '@page_cache_kill_switch']
......@@ -466,11 +466,17 @@ function watchdog_exception($type, Exception $exception, $message = NULL, $varia
*
* @see drupal_get_messages()
* @see status-messages.html.twig
* @see https://www.drupal.org/node/2774931
*
* @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) {
/* @var \Drupal\Core\Messenger\MessengerInterface $messenger */
$messenger = \Drupal::service('messenger');
$messenger->addMessage($message, $type, $repeat);
@trigger_error('drupal_set_message() is deprecated in 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);
$messenger = \Drupal::messenger();
if (isset($message)) {
$messenger->addMessage($message, $type, $repeat);
}
return $messenger->all();
}
......@@ -498,11 +504,16 @@ function drupal_set_message($message = NULL, $type = 'status', $repeat = FALSE)
*
* @see drupal_set_message()
* @see status-messages.html.twig
* @see https://www.drupal.org/node/2774931
*
* @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) {
/** @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 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. 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);
......
......@@ -6,8 +6,9 @@
*/
use Drupal\Core\DependencyInjection\ContainerNotInitializedException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Messenger\LegacyMessenger;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Static Service Container wrapper.
......@@ -100,6 +101,22 @@ class Drupal {
*/
protected static $container;
/**
* The LegacyMessenger instance.
*
* Note: this is merely used to ensure that the instance survives when
* \Drupal::messenger() is invoked. It is required to ensure that messages
* are properly transferred to the Messenger service once the container has
* been initialized. Do not store the Messenger service here.
*
* @todo Remove once LegacyMessenger has been removed before 9.0.0.
*
* @see https://www.drupal.org/node/2928994
*
* @var \Drupal\Core\Messenger\LegacyMessenger|null
*/
protected static $legacyMessenger;
/**
* Sets a new global container.
*
......@@ -757,4 +774,19 @@ public static function time() {
return static::getContainer()->get('datetime.time');
}
/**
* Returns the messenger.
*
* @return \Drupal\Core\Messenger\MessengerInterface
* The messenger.
*/
public static function messenger() {
// @todo Replace with service once LegacyMessenger is removed in 9.0.0.
// @see https://www.drupal.org/node/2928994
if (!isset(static::$legacyMessenger)) {
static::$legacyMessenger = new LegacyMessenger();
}
return static::$legacyMessenger;
}
}
<?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;
/**
* The messenger service.
*/
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);
}
/**
* {@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);
}
/**
* {@inheritdoc}
*/
public function addWarning($message, $repeat = FALSE) {
return $this->addMessage($message, static::TYPE_WARNING);
}
/**
* {@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);
}
}
......@@ -6,8 +6,6 @@
* Stores runtime messages sent out to individual users on the page.
*
* An example for these messages is for example: "Content X got saved".
*
* @internal
*/
interface MessengerInterface {
......@@ -109,11 +107,15 @@ public function all();
* or self::TYPE_ERROR.
*
* @return string[]|\Drupal\Component\Render\MarkupInterface[]
* The messages of given type.
*/
public function messagesByType($type);
/**
* Deletes all messages.
*
* @return string[]|\Drupal\Component\Render\MarkupInterface[]
* The deleted messages.
*/
public function deleteAll();
......@@ -123,6 +125,9 @@ public function deleteAll();
* @param string $type
* The messages' type. Either self::TYPE_STATUS, self::TYPE_WARNING, or
* self::TYPE_ERROR.
*
* @return string[]|\Drupal\Component\Render\MarkupInterface[]
* The deleted messages of given type.
*/
public function deleteByType($type);
......
......@@ -5,6 +5,7 @@
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\CacheableResponse;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\Session\AccountInterface;
......@@ -48,6 +49,13 @@ class SystemTestController extends ControllerBase {
*/
protected $renderer;
/**
* The messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* Constructs the SystemTestController.
*
......@@ -59,12 +67,15 @@ class SystemTestController extends ControllerBase {
* The current user.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger service.
*/
public function __construct(LockBackendInterface $lock, LockBackendInterface $persistent_lock, AccountInterface $current_user, RendererInterface $renderer) {
public function __construct(LockBackendInterface $lock, LockBackendInterface $persistent_lock, AccountInterface $current_user, RendererInterface $renderer, MessengerInterface $messenger) {
$this->lock = $lock;
$this->persistentLock = $persistent_lock;
$this->currentUser = $current_user;
$this->renderer = $renderer;
$this->messenger = $messenger;
}
/**
......@@ -75,7 +86,8 @@ public static function create(ContainerInterface $container) {
$container->get('lock'),
$container->get('lock.persistent'),
$container->get('current_user'),
$container->get('renderer')
$container->get('renderer'),
$container->get('messenger')
);
}
......@@ -99,9 +111,13 @@ public function drupalSetMessageTest() {
// Set two messages.
drupal_set_message('First message (removed).');
drupal_set_message(t('Second message with <em>markup!</em> (not removed).'));
$messages = $this->messenger->deleteByType('status');
// Remove the first.
unset($_SESSION['messages']['status'][0]);
unset($messages[0]);
foreach ($messages as $message) {
$this->messenger->addStatus($message);
}
// Duplicate message check.
drupal_set_message('Non Duplicated message', 'status', FALSE);
......
......@@ -20,10 +20,4 @@ public function testDrupalSetMessage() {
$this->assertEquals('A message: bar', (string) $messages['status'][0]);
}
protected function tearDown() {
// Clear session to prevent global leakage.
unset($_SESSION['messages']);
parent::tearDown();
}
}
<?php
namespace Drupal\KernelTests\Core\Messenger;
use Drupal\Core\Messenger\LegacyMessenger;
use Drupal\Core\Messenger\Messenger;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\KernelTests\KernelTestBase;
/**
* @group Messenger
* @coversDefaultClass \Drupal\Core\Messenger\LegacyMessenger
*/
class LegacyMessengerTest extends KernelTestBase {
/**
* Retrieves the Messenger service from LegacyMessenger.
*
* @param \Drupal\Core\Messenger\LegacyMessenger $legacy_messenger
*
* @return \Drupal\Core\Messenger\MessengerInterface|null
*/
protected function getMessengerService(LegacyMessenger $legacy_messenger) {
$method = new \ReflectionMethod($legacy_messenger, 'getMessengerService');
$method->setAccessible(TRUE);
return $method->invoke($legacy_messenger);
}
/**
* @covers \Drupal::messenger
* @covers ::getMessengerService
* @covers ::all
* @covers ::addMessage
* @covers ::addError
* @covers ::addStatus
* @covers ::addWarning
*/
public function testMessages() {
// Save the current container for later use.
$container = \Drupal::getContainer();
// Unset the container to mimic not having one.
\Drupal::unsetContainer();
/** @var \Drupal\Core\Messenger\LegacyMessenger $messenger */
// Verify that the Messenger service doesn't exists.
$messenger = \Drupal::messenger();
$this->assertNull($this->getMessengerService($messenger));
// Add messages.
$messenger->addMessage('Foobar');
$messenger->addError('Foo');
// Verify that retrieving another instance and adding more messages works.
$messenger = \Drupal::messenger();
$messenger->addStatus('Bar');
$messenger->addWarning('Fiz');
// Restore the container.
\Drupal::setContainer($container);
// Verify that the Messenger service exists.
$messenger = \Drupal::messenger();
$this->assertInstanceOf(Messenger::class, $this->getMessengerService($messenger));
// Add more messages.
$messenger->addMessage('Platypus');
$messenger->addError('Rhinoceros');
$messenger->addStatus('Giraffe');
$messenger->addWarning('Cheetah');
// Verify that all the messages are present and accounted for.
$messages = $messenger->all();
$this->assertContains('Foobar', $messages[MessengerInterface::TYPE_STATUS]);
$this->assertContains('Foo', $messages[MessengerInterface::TYPE_ERROR]);
$this->assertContains('Bar', $messages[MessengerInterface::TYPE_STATUS]);
$this->assertContains('Fiz', $messages[MessengerInterface::TYPE_WARNING]);
$this->assertContains('Platypus', $messages[MessengerInterface::TYPE_STATUS]);
$this->assertContains('Rhinoceros', $messages[MessengerInterface::TYPE_ERROR]);
$this->assertContains('Giraffe', $messages[MessengerInterface::TYPE_STATUS]);
$this->assertContains('Cheetah', $messages[MessengerInterface::TYPE_WARNING]);
}
}
......@@ -119,6 +119,9 @@ public static function getSkippedDeprecations() {
'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\Core\Entity\ContentEntityStorageBase::doLoadRevisionFieldItems()" is deprecated in Drupal 8.5.x and will be removed before Drupal 9.0.0. "\Drupal\Core\Entity\ContentEntityStorageBase::doLoadMultipleRevisionsFieldItems()" should be implemented instead. See https://www.drupal.org/node/2924915.',
'Passing a single revision ID to "\Drupal\Core\Entity\Sql\SqlContentEntityStorage::buildQuery()" is deprecated in Drupal 8.5.x and will be removed before Drupal 9.0.0. An array of revision IDs should be given instead. See https://www.drupal.org/node/2924915.',
'drupal_set_message() is deprecated in 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 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. See https://www.drupal.org/node/2774931',
'Adding or retrieving messages prior to the container being initialized was deprecated in Drupal 8.5.0 and this functionality will be removed before Drupal 9.0.0. Please report this usage at https://www.drupal.org/node/2928994.',
];
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment