diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index 35035ffa0a..ae2f8915c3 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -472,9 +472,11 @@ function watchdog_exception($type, Exception $exception, $message = NULL, $varia * Use \Drupal\Core\Messenger\MessengerInterface::addMessage() instead. */ function drupal_set_message($message = NULL, $type = 'status', $repeat = FALSE) { - @trigger_error('drupal_set_message() is deprecated 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); + @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(); - $messenger->addMessage($message, $type, $repeat); + if (isset($message)) { + $messenger->addMessage($message, $type, $repeat); + } return $messenger->all(); } @@ -509,7 +511,7 @@ function drupal_set_message($message = NULL, $type = 'status', $repeat = FALSE) * \Drupal\Core\Messenger\MessengerInterface::messagesByType() instead. */ function drupal_get_messages($type = NULL, $clear_queue = TRUE) { - @trigger_error('drupal_get_message() is deprecated 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); + @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) { diff --git a/core/lib/Drupal/Core/Messenger/BaseMessenger.php b/core/lib/Drupal/Core/Messenger/BaseMessenger.php deleted file mode 100644 index f76c295301..0000000000 --- a/core/lib/Drupal/Core/Messenger/BaseMessenger.php +++ /dev/null @@ -1,52 +0,0 @@ -addMessage($message, static::TYPE_STATUS); - } - - /** - * {@inheritdoc} - */ - public function addError($message, $repeat = FALSE) { - return $this->addMessage($message, static::TYPE_ERROR); - } - - /** - * {@inheritdoc} - */ - public function addWarning($message, $repeat = FALSE) { - return $this->addMessage($message, static::TYPE_WARNING); - } - - /** - * Convert any safe strings to markup. - * - * @param string|\Drupal\Component\Render\MarkupInterface $message - * The message. - * - * @return string|\Drupal\Component\Render\MarkupInterface - * The markup. - */ - protected function convertToMarkup($message) { - if (!($message instanceof Markup) && $message instanceof MarkupInterface) { - $message = Markup::create((string) $message); - } - return $message; - } - -} diff --git a/core/lib/Drupal/Core/Messenger/ChainedMessenger.php b/core/lib/Drupal/Core/Messenger/ChainedMessenger.php index f28de8d0b1..6ae5df97a8 100644 --- a/core/lib/Drupal/Core/Messenger/ChainedMessenger.php +++ b/core/lib/Drupal/Core/Messenger/ChainedMessenger.php @@ -3,24 +3,30 @@ namespace Drupal\Core\Messenger; /** - * Provides a chained Messenger. + * Provides a ChainedMessenger implementation. * - * This implementation uses a memory messenger for handling messages before the - * container is initialized. When initialised, it will copy over messages and - * use that thereafter. + * This implementation uses the LegacyMessenger for handling messages in a + * backwards compatible when the container has not yet been fully initialized. + * When initialised, it will use the proper Messenger service and copy any + * existing messages in the LegacyMessenger instance over to the service. * - * You should not use this class directly. Instead use the 'messenger' service. + * You should not use this class directly. Instead, you should inject the + * "messenger" service into your own services or use \Drupal::messenger() in + * procedural functions. * - * @internal + * @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\Messenger instead. */ class ChainedMessenger implements MessengerInterface { /** - * The memory messenger. + * The LegacyMessenger. * * @var \Drupal\Core\Messenger\MessengerInterface */ - protected $memoryMessenger; + protected $legacyMessenger; /** * {@inheritdoc} @@ -92,21 +98,21 @@ public function getMessenger() { if (\Drupal::hasService('messenger')) { $messenger = \Drupal::service('messenger'); - // Merge in any MemoryMessenger messages and then remove it. - if (isset($this->memoryMessenger)) { - $this->mergeMessages($this->memoryMessenger, $messenger); - unset($this->memoryMessenger); + // Merge in any LegacyMessenger messages and then remove it. + if (isset($this->legacyMessenger)) { + $this->mergeMessages($this->legacyMessenger, $messenger); + unset($this->legacyMessenger); } return $messenger; } - // Fallback to MemoryMessenger. - if (!isset($this->memoryMessenger)) { - $this->memoryMessenger = new MemoryMessenger(); + // Fallback to LegacyMessenger. + if (!isset($this->legacyMessenger)) { + $this->legacyMessenger = new LegacyMessenger(); } - return $this->memoryMessenger; + return $this->legacyMessenger; } /** diff --git a/core/lib/Drupal/Core/Messenger/LegacyMessenger.php b/core/lib/Drupal/Core/Messenger/LegacyMessenger.php new file mode 100644 index 0000000000..9831095881 --- /dev/null +++ b/core/lib/Drupal/Core/Messenger/LegacyMessenger.php @@ -0,0 +1,122 @@ +messages = &$_SESSION['messages']; + } + + /** + * {@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 (!isset($this->messages[$type])) { + $this->messages[$type] = []; + } + + 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->messages[$type])) { + $this->messages[$type][] = $message; + } + + 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->messages; + } + + /** + * {@inheritdoc} + */ + public function messagesByType($type) { + return $this->messages[$type]; + } + + /** + * {@inheritdoc} + */ + public function deleteAll() { + $messages = $this->messages; + $this->messages = []; + return $messages; + } + + /** + * {@inheritdoc} + */ + public function deleteByType($type) { + $messages = $this->messages[$type]; + $this->messages[$type] = []; + return $messages; + } + +} diff --git a/core/lib/Drupal/Core/Messenger/MemoryMessenger.php b/core/lib/Drupal/Core/Messenger/MemoryMessenger.php deleted file mode 100644 index 494a23a8e5..0000000000 --- a/core/lib/Drupal/Core/Messenger/MemoryMessenger.php +++ /dev/null @@ -1,71 +0,0 @@ - [], - MessengerInterface::TYPE_STATUS => [], - MessengerInterface::TYPE_WARNING => [], - ]; - - /** - * {@inheritdoc} - */ - public function addMessage($message, $type = self::TYPE_STATUS, $repeat = FALSE) { - $message = $this->convertToMarkup($message); - // Do not use strict type checking so that equivalent string and - // MarkupInterface objects are detected. - if ($repeat || !in_array($message, $this->messages[$type])) { - $this->messages[$type][] = $message; - } - - return $this; - } - - /** - * {@inheritdoc} - */ - public function all() { - return $this->messages; - } - - /** - * {@inheritdoc} - */ - public function messagesByType($type) { - return $this->messages[$type]; - } - - /** - * {@inheritdoc} - */ - public function deleteAll() { - $messages = $this->messages; - $this->messages = []; - return $messages; - } - - /** - * {@inheritdoc} - */ - public function deleteByType($type) { - $messages = $this->messages[$type]; - $this->messages[$type] = []; - return $messages; - } - -} diff --git a/core/lib/Drupal/Core/Messenger/Messenger.php b/core/lib/Drupal/Core/Messenger/Messenger.php index a846defd01..b8b6c3ddf3 100644 --- a/core/lib/Drupal/Core/Messenger/Messenger.php +++ b/core/lib/Drupal/Core/Messenger/Messenger.php @@ -2,15 +2,15 @@ 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. - * - * @internal */ -class Messenger extends BaseMessenger { +class Messenger implements MessengerInterface { /** * The flash bag. @@ -39,11 +39,21 @@ public function __construct(FlashBagInterface $flash_bag, KillSwitch $killSwitch $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) { - $message = $this->convertToMarkup($message); + 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))) { @@ -59,15 +69,22 @@ public function addMessage($message, $type = self::TYPE_STATUS, $repeat = FALSE) /** * {@inheritdoc} */ - public function all() { - return $this->flashBag->peekAll(); + public function addStatus($message, $repeat = FALSE) { + return $this->addMessage($message, static::TYPE_STATUS); } /** * {@inheritdoc} */ - public function messagesByType($type) { - return $this->flashBag->peek($type); + public function addWarning($message, $repeat = FALSE) { + return $this->addMessage($message, static::TYPE_WARNING); + } + + /** + * {@inheritdoc} + */ + public function all() { + return $this->flashBag->peekAll(); } /** @@ -85,4 +102,11 @@ public function deleteByType($type) { return $this->flashBag->get($type); } + /** + * {@inheritdoc} + */ + public function messagesByType($type) { + return $this->flashBag->peek($type); + } + } diff --git a/core/lib/Drupal/Core/Messenger/MessengerInterface.php b/core/lib/Drupal/Core/Messenger/MessengerInterface.php index 809c93d118..441ea6fc22 100644 --- a/core/lib/Drupal/Core/Messenger/MessengerInterface.php +++ b/core/lib/Drupal/Core/Messenger/MessengerInterface.php @@ -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 { diff --git a/core/tests/Drupal/KernelTests/Core/Messenger/ChainedMessengerTest.php b/core/tests/Drupal/KernelTests/Core/Messenger/ChainedMessengerTest.php index eae55a6549..1fd0678f52 100644 --- a/core/tests/Drupal/KernelTests/Core/Messenger/ChainedMessengerTest.php +++ b/core/tests/Drupal/KernelTests/Core/Messenger/ChainedMessengerTest.php @@ -3,7 +3,7 @@ namespace Drupal\KernelTests\Core\Messenger; use Drupal\Core\Messenger\ChainedMessenger; -use Drupal\Core\Messenger\MemoryMessenger; +use Drupal\Core\Messenger\LegacyMessenger; use Drupal\Core\Messenger\Messenger; use Drupal\Core\Messenger\MessengerInterface; use Drupal\KernelTests\KernelTestBase; @@ -33,8 +33,8 @@ public function testMessages() { // Create a new ChainedMessenger instance. $messenger = new ChainedMessenger(); - // Verify that the messenger returned is an instance of MemoryMessenger. - $this->assertInstanceOf(MemoryMessenger::class, $messenger->getMessenger()); + // Verify that the messenger returned is an instance of LegacyMessenger. + $this->assertInstanceOf(LegacyMessenger::class, $messenger->getMessenger()); // Add messages. $messenger->addMessage('Foobar'); diff --git a/core/tests/Drupal/Tests/Listeners/DeprecationListener.php b/core/tests/Drupal/Tests/Listeners/DeprecationListener.php index 99f133480f..e05c103ee2 100644 --- a/core/tests/Drupal/Tests/Listeners/DeprecationListener.php +++ b/core/tests/Drupal/Tests/Listeners/DeprecationListener.php @@ -113,8 +113,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 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 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', + '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', + '\Drupal\Core\Messenger\LegacyMessenger is deprecated in Drupal 8.5.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Messenger\Messenger instead. See https://www.drupal.org/node/2774931', ]; }