function StatusMessages::renderMessages

Same name in other branches
  1. 9 core/lib/Drupal/Core/Render/Element/StatusMessages.php \Drupal\Core\Render\Element\StatusMessages::renderMessages()
  2. 8.9.x core/lib/Drupal/Core/Render/Element/StatusMessages.php \Drupal\Core\Render\Element\StatusMessages::renderMessages()
  3. 11.x 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()

File

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

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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.