Score:0

How to make Twig variable provided by module available to a twig template of an other module?

kz flag

I am a newbie when it comes to combining drupal's API functions with a twig template.

I've created a node filed called font awesome icon The Twig variables of this field are in

fontawesomeicon.html.twig:

 * tag: the HTML tag being used to create the icon. 
 * icon: the name of the icon being used for templating.
 * style: the Font Awesome style for the icon.
 * settings: the additional Font Awesome style settings.
 * transforms: Font Awesome power transforms.
 * mask: Font Awesome mask.
 * css: Additional inline CSS styles (for duotone, etc).

So how to make them available to my-image-slider-block.html.twig

or in other words, how to make sure {% include 'fontawesomeicon.html.twig' %} would be rendered with the variables provided above?

These vars are provided by https://www.drupal.org/project/fontawesome.

I am looking forward to your answers and hope I'll be able to reproduce their content in other use cases, too.

ru flag
This is something you generally should not do in Drupal at all. Almost all Twig templates in Drupal are their own cached entities, passing variables between templates will often break caching. There are edge cases where you have to share variables (and you'll need extra PHP code to make it cache-safe), but don't use this approach as a reproduceable receipt.
ru flag
For some general hints on querying data in Twig see my answer in [Accessing node value in paragraph twig template](https://drupal.stackexchange.com/questions/306971/accessing-node-value-in-paragraph-twig-template/306975#306975)
Josh avatar
kz flag
So there is currently no safe way to provide a variable from one module to another? Could I render the output of the filed by using the nid and declare a variable for it in the blocks twig template then?
Kevin avatar
in flag
Nodes should not hold configuration like font and styles. If that is what you are after, you should be using configuration, not content.
Josh avatar
kz flag
Maybe I am on the wrong track. I just like to add a font awesome symbol selected by a font awesome icon picker filed on a node to varbase media hero slider. Of course, the module does not generate a twig variable for me, So now I am looking for an alternative way to render the output of the filed within the block provided by varbase media header within the block template. Because blocks are cached that might not be even possible by using the .theme file in my subtheme, I am just running out of ideas.
jbarrio avatar
cn flag
I find the rest of comments very useful. Trying to help, I'd say that (maybe I didn't get the point) if you want to provide "persistent" variables accros your different theme modifications, I'd say that providing the variable into a theme_preprocess would do it. If you are setting the variable in your custommodule1 and you need it to be available in custommodule2, then be sure you run your preprocess in custommodule1 before module2 is ran through the usage of hook_module_implements_alter.
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.