You are here

public static function StatusMessages::renderMessages in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Render/Element/StatusMessages.php \Drupal\Core\Render\Element\StatusMessages::renderMessages()

#lazy_builder callback; replaces placeholder with messages.

Parameters

string|null $type: Limit the messages returned by type. Defaults to NULL, meaning all types. Passed on to \Drupal\Core\Messenger\Messenger::deleteByType(). These values are supported:

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

Return value

array A renderable array containing the messages.

See also

\Drupal\Core\Messenger\Messenger::deleteByType()

1 call to StatusMessages::renderMessages()
QuickEditImageController::upload in core/modules/image/src/Controller/QuickEditImageController.php
Returns JSON representing the new file upload, or validation errors.

File

core/lib/Drupal/Core/Render/Element/StatusMessages.php, line 87

Class

StatusMessages
Provides a messages element.

Namespace

Drupal\Core\Render\Element

Code

public static function renderMessages($type = NULL) {
  $render = [];
  if (isset($type)) {
    $messages = [
      $type => \Drupal::messenger()
        ->deleteByType($type),
    ];
  }
  else {
    $messages = \Drupal::messenger()
      ->deleteAll();
  }
  if ($messages) {

    // Render the messages.
    $render = [
      '#theme' => 'status_messages',
      '#message_list' => $messages,
      '#status_headings' => [
        'status' => t('Status message'),
        'error' => t('Error message'),
        'warning' => t('Warning message'),
      ],
    ];
  }
  return $render;
}