You are here

function drupal_get_messages in Drupal 8

Same name and namespace in other branches
  1. 4 includes/bootstrap.inc \drupal_get_messages()
  2. 5 includes/bootstrap.inc \drupal_get_messages()
  3. 6 includes/bootstrap.inc \drupal_get_messages()
  4. 7 includes/bootstrap.inc \drupal_get_messages()

Returns all messages that have been set with drupal_set_message().

Parameters

string $type: (optional) Limit the messages returned by type. Defaults to NULL, meaning all types. These values are supported:

  • NULL
  • 'status'
  • 'warning'
  • 'error'

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 value

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.

Deprecated

in drupal:8.5.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Messenger\MessengerInterface::all() or \Drupal\Core\Messenger\MessengerInterface::messagesByType() instead.

See also

drupal_set_message()

status-messages.html.twig

https://www.drupal.org/node/2774931

1 call to drupal_get_messages()
DrupalSetMessageTest::testDrupalSetMessage in core/tests/Drupal/KernelTests/Core/Common/DrupalSetMessageTest.php
The basic functionality of drupal_set_message().

File

core/includes/bootstrap.inc, line 545
Functions that need to be loaded on every Drupal request.

Code

function drupal_get_messages($type = NULL, $clear_queue = TRUE) {
  @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);
      }
      if (isset($messages[$type])) {
        return [
          $type => $messages[$type],
        ];
      }
    }
    else {
      if ($clear_queue) {
        $messenger
          ->deleteAll();
      }
      return $messages;
    }
  }
  return [];
}