2

I have content type A. It has field reference to content type B.

I'm rendering referenced node B from content type A twig template like:

{{ content.field_referenced_object }}

And that works well.

I also render some type A filed inside it's template like:

{{ content.field_some }}

which also works.

Problem is that I need that field_some value to be displayed from referenced object B template instead of object A template, to optimize page markup. How can I pass value from content type A twig template to content type B twig template?

It twig it can be done when including template like:

{% include 'template.html' with {'foo': 'bar'} %}

But is there some analogy for Drupal 8 theming?

  • 2
    TL,DR: No. Long version: Conceptionally entity render arrays are self-contained, cachable units. The Drupal way would be either preprocessing and a custom cache context based on the referencing node, or a views block with a contextual filter. But IMHO both ways are terrible workarounds, your Twig snippet is the better technique. – Hudri May 27 at 13:09
2

TL,DR: No.

Long version:

Conceptionally entity render arrays are self-contained, cachable units. The Drupal way would be either preprocessing and a custom cache context based on the referencing node, or a views block with a contextual filter.

But IMHO both ways are terrible workarounds, your Twig snippet {% include ... with ... %} is the better technique.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.