0

How do I call a page.html.twig template variable from the book-tree.html.twig template.

Using kint, I have found the variable, nid as in the screenshot below:

The 'nid' is highlighted, but it is the 'x-default value that is required.

enter image description here

This variable will be used in a set statement, comparing it to the item.original_link.nid to set an active class.

This page nid is not available within the book-tree.html.twig template.

As per Answer 1, I have created a _preprocess function in my subthemes .theme file as below:

function subtheme_bs_preprocess_book_tree(&$variables) {
 $variables['page_nid'] = 'Add code to get the node id variable';
}

The variable page_nid now shows up in the book-tree.html.twig array. The result is 'page_id' => string(36) "Add code to get the node id variable" which is the static text I added.

I would like help on how to call the nid value from the node.html.twig template. I have tried several combinations:

$variables['page_id'] = $variables['page']['content']['book_navigation']['#book_link']['nid'] But this and many other compinations I have tried all end with a:

Notice: Undefined index: page in subtheme_bs_preprocess_book_tree()

Maybe I need to call an Available method but have no clue as to how this is done.

Below is a screenshot of the {{ kint() }} output from the top of the node.html.twig file.

the value I need to target is the 115 in this line 'x-default' => value (3) 115.

Many thanks in advance.

enter image description here

1 Answer 1

0

The book-tree.twig is rendered in its own scope. It does not "know" any of the context it is being rendered in, so it can't tap into the variables from the page twig per se. It only gets the variables you give it. So you could implement a THEME_preprocess_book_tree(&$variables) hook to add it in. $variables['page_nid'] = // your code here will make it available in twig as {{ page_nid }}.

However at the risk of being "that annoying guy" who tells you not to do things instead of just answering your question

This variable will be used in a set statement, comparing it to the item.original_link.nid to set an active class.

Personally I would eschew this approach as it ends up placing logic inside the presentation layer. I would rather use the hook to compare item nids to page nid, and add the active class to that item's attributes as appropriate. That way the twig can be kept extremely "stupid", and simply dump out whatever attributes for all items, and those attributes will magically contain (or not contain) the appropriate classes.

1
  • Thanks for being "that annoying guy" Having read many other posts about twig templates, I will now have to read a lot more to understand the _preprocess process. Many thanks. Commented Mar 17, 2020 at 13:57

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.