Pass a value from a block preprocess hook to a twig template that inserts that block I use the D8 book module to manage magazines and their articles.

In the node--magazine.html.twig template for a magazine, I use the built-in book_navigation block to add a table of contents. This is done by drupal_block("book_navigation", {block_mode: "book pages"}). Works fine.

For certain "featured" articles in the table of contents, I want to display an image. I use MYTHEME_preprocess_book_tree(&amp;$variables) to add the image url to the items array in $variables. I've sub-themed book-tree-html.twig to get the image url from the items array and include the image in the table of contents. Works fine.

BUT, I need to know in node--magazine.html.twig whether there are any featured articles in the table of contents to present a proper heading on the page.

QUESTION: What is the best way to pass a variable from THEME_preprocess_book_tree() to node--magazine.html.twig? It's not a direct connection. Getting preprocess variables in book-tree.html.twig is easy, but I need to pass a variable through to node--magazine.html.twig, where the book tree is inserted.

What I have tried:


In the book-tree.html.twig template, if there is a featured article, I add a hidden "featured-article" label to the html and then in the magazine template, I test for its presence via {% if "featured-article" in &lt;rendered block html&gt; %}. This works, but it feels unsavory.
In MYTHEME_preprocess_book_tree(), I add $variables['featured'] = $feature_count; and in the magazine template I {{ dump(featured) }}. This doesn't work. I just get NULL. I'm not sure what--if any--connection there is between the $variables array available in the preprocess hook and variables available in the magazine template.
In MYTHEME_preprocess_book_tree(), I could set a global variable and then in the magazine template, call a custom twig extension to check the value of that variable. I haven't tried this yet, but it seems like this should work. Just seems like there should be a more elegant way.
After a lot more digging and trying things without success, I ended up solving the problem of passing a value from a block's preprocess hook to a twig template where the block is inserted as per my third option above. Here's what I did:


Follow the instructions at http://www.tothenew.com/blog/overview-of-twig-extentions-in-drupal-8 to create a twig extension. This wasn't very difficult, especially since I had already created a custom module. I just had to create a MYMODULE.services.yml file and a TwigExtension.php file as instructed.
In the twig extension file, create functions setTwigValue($name, $value) and getTwigValue($name) using the php $GLOBALS array for storage. Again, the above-mentioned instructions made this pretty easy.
In the preprocess_book_tree hook, count the number of feature articles and then save the count by calling

Drupal\MYMODULE\TwigExtension::setTwigValue('feature_count', $feature_count);
In the node--magazine.html.twig template, get the saved value using

{% set feature_count = getTwigValue('feature_count') %}


I'm happy to learn of better/safer ways to accomplish what I've done. But at least it works and it's pretty clean.