1

I am developing a theme in Drupal 8 and I have 2 twig files. One is page.html.twig and the other is status-messages.html.twig.

The problem is that status-messages has a variable named message_list (an array) but this variable is protected, so other files don't have access to it. I have tried to call this variable from theme_preprocess_page() like so $variables['testvar'] = $variables['message_list']; with result of $variables['testvar'] = null.

In the next step I tried to copy the functionality of status-messages.html.twig from Drupal\Core\Render\Element\StatusMessages to mythemem.theme file, like so:

use Drupal\Core\Render\Element\StatusMessages;

function MYTHEME_preprocess_page(&$variables) {

  // Create Dynamic path for project root elements (in general files that are in our theme project)
  $variables['theme_path'] = base_path() . $variables['directory'];

  // this here gives us the array of renderMessages() but  '#message_list' has no data in it
  $variables['testvar'] = StatusMessages::renderMessages(); 
}


function mythemem_preprocess_status_messages(&$variables) {

  // this here dose gives the value but it can be accessed  only in status-messages.html.twig
  $variables['mymessages'][] = $variables['message_list'];

  // same as in mythemem_preprocess.page
  $variables['testvar'] = StatusMessages::renderMessages(); 
}

My aim here is to understand how to correctly use .theme files, how to pass protected variables from one preprocess function to another.


To be more clear I would like to know/understand how to get the same effect for my custom.html.twig file as it is here

To achieve this, I want to take $variables['message_list'] to process it as I want.

10
  • Messages are now in a block, no longer in the page template, see drupal.org/node/2446647. And you can't access the variables from the block in page directly. You render the messages by placing the block in a region and the region on the page.
    – 4uk4
    Feb 21, 2017 at 13:47
  • @4k4 After some debugging with kint, i am sure that there is no such variable like 'message_list' Feb 21, 2017 at 14:12
  • Are you trying to modify the message_list, duplicate it or move it at a specific ?
    – Cawet
    Feb 21, 2017 at 14:42
  • @Cawet i am trying to duplicate this variable... but it is protected, is there a way to duplicate protected variable and pas it to the higher level than it is right now (as i know preprocess.page function is executed before preprocess_status_messages. As level, status message is in a lower level). if i am wrong pleas correct me Feb 21, 2017 at 14:57
  • 1
    Please explain why you need to get this var, your final point. I'm sure there is some other logic to achieve what you need. This could be put it at a other place, simply altering it, or many other thing.
    – Cawet
    Feb 21, 2017 at 18:50

1 Answer 1

0

well after finking a lot about this problem, i realized that i don't wont to duplicate this variable at all.

lets start from beginning i wanted to copy the $variables['message_list'] from status-messages.html.twig to make my custom layout for different situation.

for example i wanted to have different layout/design of status message on my custom login page and another layout/design for my custom register page. but now i understend that i can have this two in one place (in status-messages.html.twig) i can check if my URL is /login tean with help of twig if condition i can render one layout and if my URL is /register then i could have second layout! and in aver situation u would have default layout.

is that correct? how would you solve the problem if you ware in my place

Your Answer

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

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