diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index dd68804389..9b04cf3b6e 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -505,7 +505,8 @@ function drupal_set_message($message = NULL, $type = 'status', $repeat = FALSE) */ function drupal_get_messages($type = NULL, $clear_queue = TRUE) { /** @var \Drupal\Core\Messenger\MessengerInterface $messenger */ - if (($messenger = \Drupal::hasService('messenger') ? \Drupal::service('messenger') : NULL) && $messages = $messenger->all()) { + if ((($messenger = \Drupal::hasService('messenger')) ? \Drupal::service('messenger') : NULL) + && $messages = $messenger->all()) { if ($type) { if ($clear_queue) { $messenger->deleteByType($type); diff --git a/core/lib/Drupal/Core/Messenger/LegacyMessenger.php b/core/lib/Drupal/Core/Messenger/LegacyMessenger.php index be46c44b5f..69c229286e 100644 --- a/core/lib/Drupal/Core/Messenger/LegacyMessenger.php +++ b/core/lib/Drupal/Core/Messenger/LegacyMessenger.php @@ -9,7 +9,7 @@ * A legacy implementation of the messenger interface. * * @internal - * @deprecated Deprecated as of Drupal 8.4. + * @deprecated Deprecated as of Drupal 8.5.x */ class LegacyMessenger implements MessengerInterface { @@ -17,7 +17,7 @@ class LegacyMessenger implements MessengerInterface { * {@inheritdoc} */ public function addMessage($message, $type = self::TYPE_STATUS, $repeat = FALSE) { - _drupal_set_message($message, $type, $repeat); + $this->setMessage($message, $type, $repeat); } /** @@ -45,106 +45,107 @@ public function addWarning($message, $repeat = FALSE) { * {@inheritdoc} */ public function all() { - return _drupal_get_messages(NULL, FALSE); + return $this->getMessages(NULL, FALSE); } /** * {@inheritdoc} */ public function messagesByType($type) { - return _drupal_get_messages($type, FALSE); + return $this->getMessages($type, FALSE); } /** * {@inheritdoc} */ public function deleteAll() { - return _drupal_get_messages(NULL, TRUE); + return $this->getMessages(NULL, TRUE); } /** * {@inheritdoc} */ public function deleteByType($type) { - return _drupal_get_messages($type, TRUE); + return $this->getMessages($type, TRUE); } -} + /** + * @internal + */ + private function setMessage($message = NULL, $type = 'status', $repeat = FALSE) { + if (isset($message)) { + if (!isset($_SESSION['messages'][$type])) { + $_SESSION['messages'][$type] = []; + } -/** - * @internal - */ -function _drupal_set_message($message = NULL, $type = 'status', $repeat = FALSE) { - if (isset($message)) { - if (!isset($_SESSION['messages'][$type])) { - $_SESSION['messages'][$type] = []; - } + // Convert strings which are safe to the simplest Markup objects. + if (!($message instanceof Markup) && $message instanceof MarkupInterface) { + $message = Markup::create((string) $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, $_SESSION['messages'][$type])) { + $_SESSION['messages'][$type][] = $message; + } - // Do not use strict type checking so that equivalent string and - // MarkupInterface objects are detected. - if ($repeat || !in_array($message, $_SESSION['messages'][$type])) { - $_SESSION['messages'][$type][] = $message; + // Mark this page as being uncacheable. + \Drupal::service('page_cache_kill_switch')->trigger(); } - // Mark this page as being uncacheable. - \Drupal::service('page_cache_kill_switch')->trigger(); + // Messages not set when DB connection fails. + return isset($_SESSION['messages']) ? $_SESSION['messages'] : NULL; } - // Messages not set when DB connection fails. - return isset($_SESSION['messages']) ? $_SESSION['messages'] : NULL; -} - -/** - * Returns all messages that have been set with drupal_set_message(). - * - * @param string $type - * (optional) Limit the messages returned by type. Defaults to NULL, meaning - * all types. These values are supported: - * - NULL - * - 'status' - * - 'warning' - * - 'error' - * @param bool $clear_queue - * (optional) If this is TRUE, the queue will be cleared of messages of the - * type specified in the $type parameter. Otherwise the queue will be left - * intact. Defaults to TRUE. - * - * @return array - * An associative, nested array of messages grouped by message type, with - * the top-level keys as the message type. The messages returned are - * limited to the type specified in the $type parameter, if any. If there - * are no messages of the specified type, an empty array is returned. See - * drupal_set_message() for the array structure of individual messages. - * - * @see drupal_set_message() - * @see status-messages.html.twig - * - * @internal - * @deprecated Deprecated as of Drupal 8.4. - * Use \Drupal::service('messenger')->getMessages() or - * \Drupal::service('messenger')->getMessagesByType() instead. - */ -function _drupal_get_messages($type = NULL, $clear_queue = TRUE) { - if ($messages = _drupal_set_message()) { - if ($type) { - if ($clear_queue) { - unset($_SESSION['messages'][$type]); - } - if (isset($messages[$type])) { - return [$type => $messages[$type]]; + /** + * Returns all messages that have been set with drupal_set_message(). + * + * @param string $type + * (optional) Limit the messages returned by type. Defaults to NULL, meaning + * all types. These values are supported: + * - NULL + * - 'status' + * - 'warning' + * - 'error' + * @param bool $clear_queue + * (optional) If this is TRUE, the queue will be cleared of messages of the + * type specified in the $type parameter. Otherwise the queue will be left + * intact. Defaults to TRUE. + * + * @return array + * An associative, nested array of messages grouped by message type, with + * the top-level keys as the message type. The messages returned are + * limited to the type specified in the $type parameter, if any. If there + * are no messages of the specified type, an empty array is returned. See + * drupal_set_message() for the array structure of individual messages. + * + * @see drupal_set_message() + * @see status-messages.html.twig + * + * @internal + * @deprecated Deprecated as of Drupal 8.5. + * Use \Drupal::service('messenger')->getMessages() or + * \Drupal::service('messenger')->getMessagesByType() instead. + */ + private function getMessages($type = NULL, $clear_queue = TRUE) { + if ($messages = $this->setMessage()) { + if ($type) { + if ($clear_queue) { + unset($_SESSION['messages'][$type]); + } + if (isset($messages[$type])) { + return [$type => $messages[$type]]; + } } - } - else { - if ($clear_queue) { - unset($_SESSION['messages']); + else { + if ($clear_queue) { + unset($_SESSION['messages']); + } + return $messages; } - return $messages; } + return []; } - return []; + } +