Score:1

empty variables when i use a region template for a specific node

cn flag

My goal is to serve the drupal header region on a specific URL so another application can get my drupal header and consume it.

my menu is well working, and is called in region--header.html.twig like that :

<header class="header {% if is_admin %} header-isadmin{% endif %}">
<div class="header__overlay"></div>
<div class="header__mask"></div>
{{ elements.altabigmenu }}
</header>

So, i created a page with a specific URL and used HOOK_theme_suggestions_node_alter to change its template with my region :

if ($node != NULL) {
    $url_alias = Drupal::service('path_alias.manager')->getAliasByPath('/node/' . $node->id());
}

if (isset($url_alias)) {
    if ($url_alias == "/ap/nav/header") {
      $suggestions[] = "region__header";
    }
}

in the end my node is rendered using the region template, but the {{ elements.altabigmenu }} tag returns nothing. My first guess was to inject the menu via the $variables array in preprocess_node, but i don't get variables declared in HOOK_preprocess_node

unusedspoon avatar
aq flag
Your likely problem is that you're trying to render a region in the context of a node, so none of the regions variables are set. As an alternative you could create a custom controller in a custom module which returns the relevant render array for the header to be themed
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.