5

In Drupal 7 I was able to do something like this:

<?php echo $messages; ?>

It seems this variable is no more in Drupal 8.

{{messages}} // This returns NULL on {{dump(messages)}}.

I basically have a page--user--login.html.twig where I output the page.content and above that I want the messages to be displayed, in case there are errors on the page and not the entire header.

// Not desirable
{% if page.header %}
   {% block header %}
     <div class="col-sm-12" role="heading">
         {{ page.header }}
      </div>
   {% endblock %}
{% endif %}

What is the correct variable name for <?php echo $messages ?> in Drupal 8?

1
  • 2
    Status Messages is a block now, it'll appear in whatever region you place it in
    – Clive
    Commented Feb 4, 2016 at 9:01

3 Answers 3

8

By default the messages are in the highlighted region, put this in your page twig:

{{ page.highlighted }}

If you changed the block layout, go to

admin/structure/block

and check in which region you placed the status message block. When it is disabled, move it back into a region.

1
  • It seems mine was in another region and I placed it back into the highlighted region it's the only one there so it prints nicely. Is there a way to print out a certain block from a region rather than printing out the entire region? i.e {{ page.header.status-message }}
    – veescode
    Commented Feb 4, 2016 at 14:46
1

I display block with status messages by the next way:

  1. in hook_preprocess_page() add custom variable and put in messages block

function my_theme_preprocess_page(array &$variables) { // add messages block to front page if ($variables['is_front']) { $block_msg = \Drupal\block\Entity\Block::load('dtt_theme_messages'); $variables['custom_system_messages'] = \Drupal::entityTypeManager() ->getViewBuilder('block') - >view($block_msg); } }

  1. in twig template, for my example page--front.html.twig, I can insert block with messages in any place

{{ custom_system_messages }}

1

you can print the message by:

{{ page.highlighted.bartik_messages }}

bartik_messages is the Machine name of this block for current themeenter image description here

In general, you could use the following pattern:

{{ page.MESSAGES_BLOCK_REGION.THEME_NAME_messages }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.