1

I'm trying to figure out how I can render values from nested Paragraphs. Basically I have field/values from a Paragraph within a Paragraph I have tried experimenting and searching for answers but I can't seem display the values from the nested Paragraphs to the twig template.

enter image description here

enter image description here

enter image description here

Here is the sample of the code below that I have been working on

First Paragraph     
{% for paragraph in node.field_sidebar_area %}

    <h2>{{ paragraph.entity.field_label.value }}</h2>

    <ul>

        {% for block_query in paragraph.entity.field_block_query  %}

            {{ block_query.entity.body.value | raw }}

        {% endfor %}

    </ul>

    <ul>
         {% for link in paragraph.entity.field_link_item %}

         <li>{{ link.url }}</li>
         <li>{{ link.title }}</li>

         {% endfor %}
    </ul>

    Second Paragraph
    {% for object in paragraph.node.field_sidebar_content %}

        {{ object.entity.field_label.value }}

    {% endfor }

{% endfor %}
  • What I'm asking for here is trying to print values from nested paragraphs.. – clestcruz Jun 19 '18 at 11:12
  • The code you posted isn’t even working though? That looks like Menu twig code which won’t work here. You render paragraphs like normal fields, they have their own twigs and preprocess hooks as well. – Kevin Jun 19 '18 at 13:07
  • @Kevin the field_menu is just a machine name, it's not actually a menu. – clestcruz Jun 19 '18 at 14:23
  • You are using {{ node.field... }} variables, they only work in node--xxx.hml.twig templates, not in paragaph templates. Paragraph have their own paragraph--xxx.html.twig templates, and you need to access fields in there with {{ paragraph.field... }} – Hudri Jun 19 '18 at 14:37
  • @Hudri I'm using {{ node.field }} on my page.html.twig. Currently printing the values / data to my page.html.twig template – clestcruz Jun 20 '18 at 1:28
1

I am also curious about the best way to do this. While I'm still trying to figure out what that is, here is what I've done:

I've enabled the Twig Tweak module, and inside the first paragraph template, I pull in the second paragraph with a selected view mode.

{{ drupal_entity('paragraph', content.field_sidebar_area[0]['#paragraph'].id(), 'MY_CUSTOM_VIEW_MODE') }}

And then in the template for the second paragraph, I can adjust the Twig as I please. Note that the naming convention would be:

paragraph--PARAGRAPH-TYPE--MY-CUSTOM-VIEW-MODE.html.twig.

Best of luck!

  • Sorry for my ignorance how do you fetch or print the paragraphs on the page.html.twig – clestcruz Jun 20 '18 at 5:54
  • You should be able to use that same code, but you could replace the 'MY_CUSTOM_VIEW_MODE' with 'default'. Note that this requires Twig Tweak to be enabled. – andileco Jun 20 '18 at 14:45

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