diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index 7bfa55c..4e9803a 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -514,10 +514,10 @@ function watchdog_exception($type, Exception $exception, $message = NULL, $varia * drupal_set_message(t('An error occurred and processing did not complete.'), 'error'); * @endcode * - * @param string $message + * @param array|string $message * (optional) The translated message to be displayed to the user. For * consistency with other messages, it should begin with a capital letter and - * end with a period. + * end with a period or a render array. * @param string $type * (optional) The message's type. Defaults to 'status'. These values are * supported: @@ -561,6 +561,11 @@ function drupal_set_message($message = NULL, $type = 'status', $repeat = FALSE) $_SESSION['messages'][$type] = array(); } + // When provided a render array, render it before storing it on the session. + if (is_array($message)) { + $message = \Drupal::service('renderer')->render($message); + } + $new = array( 'safe' => SafeMarkup::isSafe($message), 'message' => $message, diff --git a/core/modules/system/src/Tests/Bootstrap/DrupalSetMessageTest.php b/core/modules/system/src/Tests/Bootstrap/DrupalSetMessageTest.php index 37a1663..bf130b5 100644 --- a/core/modules/system/src/Tests/Bootstrap/DrupalSetMessageTest.php +++ b/core/modules/system/src/Tests/Bootstrap/DrupalSetMessageTest.php @@ -43,4 +43,12 @@ function testDuplicatedMessages() { $this->assertNoUniqueText('Duplicated message'); } + /** + * Tests setting render array messages. + */ + function testRenderArrayMessages() { + $this->drupalGet('system-test/drupal-set-message'); + $this->assertText('Render array'); + } + } 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 5ac7ca0..3b69300 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 @@ -103,6 +103,9 @@ public function drupalSetMessageTest() { drupal_set_message('First message (removed).'); drupal_set_message('Second message (not removed).'); + // Add a render array message. + drupal_set_message(['#markup' => 'Render array']); + // Remove the first. unset($_SESSION['messages']['status'][0]); @@ -112,6 +115,7 @@ public function drupalSetMessageTest() { drupal_set_message('Duplicated message', 'status', TRUE); drupal_set_message('Duplicated message', 'status', TRUE); + return []; }