3 namespace Drupal\Core\Render\Element;
6 * Provides a messages element.
8 * Used to display results of \Drupal::messenger()->addMessage() calls.
12 * $build['status_messages'] = [
13 * '#type' => 'status_messages',
17 * @RenderElement("status_messages")
19 class StatusMessages extends RenderElement {
24 * Generate the placeholder in a #pre_render callback, because the hash salt
25 * needs to be accessed, which may not yet be available when this is called.
27 public function getInfo() {
29 // May have a value of 'status' or 'error' when only displaying messages
30 // of that specific type.
33 get_class() . '::generatePlaceholder',
39 * #pre_render callback to generate a placeholder.
41 * @param array $element
45 * The updated renderable array containing the placeholder.
47 public static function generatePlaceholder(array $element) {
49 '#lazy_builder' => [get_class() . '::renderMessages', [$element['#display']]],
50 '#create_placeholder' => TRUE,
53 // Directly create a placeholder as we need this to be placeholdered
54 // regardless if this is a POST or GET request.
55 // @todo remove this when https://www.drupal.org/node/2367555 lands.
56 return \Drupal::service('render_placeholder_generator')->createPlaceholder($element);
60 * #lazy_builder callback; replaces placeholder with messages.
62 * @param string|null $type
63 * Limit the messages returned by type. Defaults to NULL, meaning all types.
64 * Passed on to \Drupal\Core\Messenger\Messenger::deleteByType(). These
65 * values are supported:
72 * A renderable array containing the messages.
74 * @see \Drupal\Core\Messenger\Messenger::deleteByType()
76 public static function renderMessages($type = NULL) {
80 $type => \Drupal::messenger()->deleteByType($type),
84 $messages = \Drupal::messenger()->deleteAll();
88 // Render the messages.
90 '#theme' => 'status_messages',
91 '#message_list' => $messages,
92 '#status_headings' => [
93 'status' => t('Status message'),
94 'error' => t('Error message'),
95 'warning' => t('Warning message'),