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


 * 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.

  • 1
    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.
    – Hudri
    Oct 18, 2021 at 16:16
  • For some general hints on querying data in Twig see my answer in Accessing node value in paragraph twig template
    – Hudri
    Oct 18, 2021 at 16:19
  • 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?
    – Josh
    Oct 18, 2021 at 17:24
  • Nodes should not hold configuration like font and styles. If that is what you are after, you should be using configuration, not content.
    – Kevin
    Oct 18, 2021 at 18:14
  • 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.
    – Josh
    Oct 18, 2021 at 18:58


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.