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.