Adding a status message can be useful when providing contextual help, or whenever you may want to deliver a message using the site theme somewhere other than the page top.

This code defines three things needed by the status-messages.html.twig template:

  • Sets a new, empty Attribute object using twig's built in function. Twig will populate this in the template.
  • Sets the status_headings array. This is needed to render the structure of the message block.
  • Sets the message_list array.
{% set attributes = create_attribute() %} 
{% set status_headings = { 'status': 'Status message'|t } %} 
{% set message_list = {'status': ['This is a basic status message']} %}
{% include '@classy/misc/status-messages.html.twig' %}

I'm only passing in the heading I need for this message.

The status_headings are usually passed in when rendering the template using the status_messages theme.

$render = [ 
   '#theme' => 'status_messages', 
   '#message_list' => $messages,   
   '#status_headings' => [ 
    'status' => t('Status message'), 
    'error' => t('Error message'), 
    'warning' => t('Warning message'), 
Last Updated February 17th, 2019