drupal-modulesdrupal-8drupal-themingdrupal-templates

Accessing the referencing / parent element in twig (paragraphs)


I have an entity reference field inside a (parent-)paragraph, which references multiple child-paragraphs.

Is it possible to access field values of the referencing paragraph in the children's (referenced paragraph's) twig templates?

Actually I'm just trying to count the total referenced items within one of the referenced item's twig templates itself. So I want to count its siblings + 1, if you want.

I'm aware of the fact that I could preprocess this in a module, but I would like to know if this is possible in twig.


Solution

  • In twig:

    {% set paragraph_parent = paragraph.getParentEntity() %}
    {% set width = paragraph_parent.field_width.0.value %}
    
    <div class="{{ width }}">{{ content.field_images }}</div>