1

I have a paragraph that has a field of type paragraph reference to sub-paragraphs.

Within my template, I need to access these sub-paragraphs and render the entity element. I have a loop that loops through a list of Drupal\paragraphs\Entity\Paragraph elements.

I need to render the paragraph markup. Is there a function?

I tried item|render and item.view(), but neither of them worked.

0

You should be able to render the sub paragraph - without any loop, just like every other field - with

{{ content.field_name }}

If you really need the loop for some special markup you can try something like

{% for key, item in content.field_name if key|first != '#' %}
  <div class="some_additional_markup">
    {{ item }}
  </div>
{% endfor %}
  • Thanks Hudri. Your script does give me the exact list of results as my loop. A list of Paragraphs. {{ item }} is an object of type Drupal\paragraphs\Entity\Paragraph but this is not rendered. I need to turn them into markups. In D7 i used to use entity_view('paragraphs_item', $entities, 'full'); How to do this in D8? Is there a way to achieve this in Twig? Or should I create a Twig Function as the Twig context is not able to create markups of entities? – mogio Nov 19 '17 at 23:06
  • 2
    Are you using {{ content.items }} or {{ paragraph.items }}? The first should provide the render arrays, the latter should give you the objects. If that's not the case, it seems like an error to me. Regarding entity_view, I suggest to take a look at at the Twig tweaks module – Hudri Nov 20 '17 at 7:09
  • yeah u are right. Weird... tested it yesterday night and I could not render it. Thanks again – mogio Nov 20 '17 at 10:14
  • ok. but how to access the field of the paragraph as markups? {% set item_field_tax_1 = item|first.field_tax_1 %} {{ item_field_tax_1 }} Gives me the error "Object of type Drupal\Core\Field\EntityReferenceFieldItemList cannot be printed". – mogio Nov 20 '17 at 10:43
  • ok to anybody who will have this issue as well. Please use this module drupal.org/project/twig_render_this. It creates a twig extension that allows you to render Field Elements directly without having to use the predefined content element. Pretty much what I wanted to write myself. – mogio Nov 20 '17 at 11:04
0

If you want to use a field value in child paragraph within the parent paragraph, you can use this (replace of course field_with_paragraph_childs and field_name_field with the actual fieldnames):

{% for key, item in paragraph.field_with_paragraph_childs if key|first != '#' %}
        <div class="some_additional_markup">
                {{ item.entity.field_name_field.value }}
        </div>
{% endfor %}

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.