I'm building a custom block type with referenced nodes, on the "view" of the block type I configured to show the full node instead just links. And I want to access from the node--type.html.twig to some of the fields of the parent block (when the block is rendered). How we can do that?? is there any other good approach to doing this??


When you are inside the children node, you don't see what is around you. The children node is build and cached on its own. The entity reference is a n:m relationship. There can be more than one parent. So from the data modelling there is no easy solution. Better look for a solution from parent to children, not backwards.


You can do it with a bit of custom code but as 4k4 said, you need to take care of caching.

In preprocess of the block template, you have the rendered fields available and for the reference field , you can put something additional in it (`$variables['elements']['content']['yourfield'][0]['#something'] = $your_stuff). That should then again be available preprocess/templates for the block template.

If you do that, you likely need to disable render caching, you can do so by removing the #cache keys in the same place. unset($variables['elements']['content']['yourfield'][0]['#cache']['keys']).

Entirely untested.

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.