diff --git a/core/lib/Drupal/Core/Messenger/Messenger.php b/core/lib/Drupal/Core/Messenger/Messenger.php
index e7aab7724c..e0707304ad 100644
--- a/core/lib/Drupal/Core/Messenger/Messenger.php
+++ b/core/lib/Drupal/Core/Messenger/Messenger.php
@@ -2,6 +2,8 @@
namespace Drupal\Core\Messenger;
+use Drupal\Component\Render\MarkupInterface;
+use Drupal\Core\Render\Markup;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
/**
@@ -32,7 +34,16 @@ public function __construct(FlashBagInterface $flash_bag) {
* {@inheritdoc}
*/
public function addMessage($message, $type = self::TYPE_STATUS, $repeat = FALSE) {
- $this->flashBag->add($type, $message);
+ // Convert strings which are safe to the simplest Markup objects.
+ 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);
+ }
+ return $this;
}
/**
diff --git a/core/lib/Drupal/Core/Messenger/MessengerInterface.php b/core/lib/Drupal/Core/Messenger/MessengerInterface.php
index 216835bcea..217da76575 100644
--- a/core/lib/Drupal/Core/Messenger/MessengerInterface.php
+++ b/core/lib/Drupal/Core/Messenger/MessengerInterface.php
@@ -109,11 +109,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 +127,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);
diff --git a/core/modules/system/tests/modules/system_test/src/Controller/SystemTestController.php b/core/modules/system/tests/modules/system_test/src/Controller/SystemTestController.php
index 5e49c2403b..6ed439e632 100644
--- a/core/modules/system/tests/modules/system_test/src/Controller/SystemTestController.php
+++ b/core/modules/system/tests/modules/system_test/src/Controller/SystemTestController.php
@@ -96,13 +96,6 @@ public function mainContentFallback() {
* Empty string, we just test the setting of messages.
*/
public function drupalSetMessageTest() {
- // Set two messages.
- drupal_set_message('First message (removed).');
- drupal_set_message(t('Second message with markup! (not removed).'));
-
- // Remove the first.
- unset($_SESSION['messages']['status'][0]);
-
// Duplicate message check.
drupal_set_message('Non Duplicated message', 'status', FALSE);
drupal_set_message('Non Duplicated message', 'status', FALSE);
diff --git a/core/modules/system/tests/src/Functional/Bootstrap/DrupalSetMessageTest.php b/core/modules/system/tests/src/Functional/Bootstrap/DrupalSetMessageTest.php
index 976e8bebfe..ff207b5f3f 100644
--- a/core/modules/system/tests/src/Functional/Bootstrap/DrupalSetMessageTest.php
+++ b/core/modules/system/tests/src/Functional/Bootstrap/DrupalSetMessageTest.php
@@ -22,11 +22,7 @@ class DrupalSetMessageTest extends BrowserTestBase {
* Tests drupal_set_message().
*/
public function testDrupalSetMessage() {
- // The page at system-test/drupal-set-message sets two messages and then
- // removes the first before it is displayed.
$this->drupalGet('system-test/drupal-set-message');
- $this->assertNoText('First message (removed).');
- $this->assertRaw(t('Second message with markup! (not removed).'));
// Ensure duplicate messages are handled as expected.
$this->assertUniqueText('Non Duplicated message');
diff --git a/core/tests/Drupal/KernelTests/Core/Common/DrupalSetMessageTest.php b/core/tests/Drupal/KernelTests/Core/Common/DrupalSetMessageTest.php
index 59470e6563..7a15fc3e04 100644
--- a/core/tests/Drupal/KernelTests/Core/Common/DrupalSetMessageTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Common/DrupalSetMessageTest.php
@@ -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();
- }
-
}