I have a paragraph (Entity reference revisions) with multiple items.

I would like to display the item list rendered.

I only get the paragraph as element['#object'] (in a pseudo field).

Unfortunately: entity_reference_revisions\EntityReferenceRevisionsFieldItemList cannot be printed.

I thought about looping a Twig Tweak rendering: {{ drupal_entity('paragraph', ID, 'default') }}

But than I would have to get the individual ID of each item in the loop.

How could I manage this? Or is there a better solution at all?

  • You won't find field deltas in the paragraph template, render the item list in field--field-paragraph.html.twig. – 4k4 Aug 12 at 6:37

The field item list belongs to a field, so you need to render the list in a field template.

Here's a basic example of a field template that renders each item keyed by its position in the list:

{# field--yourfieldname.html.twig #}
  {% for item in items %}
    <li data-index="{{ loop.index }}">
      {{ item.content }}
  {% endfor %}

The field item list cannot do anything to item.content because it has already been rendered into HTML.

If instead you want to render each paragraph item keyed by its entity ID (as opposed to its list index) then you should do that in the entity template:

{# paragraph--yourparagraphtypename.html.twig #}
<div data-pid="{{ paragraph.id() }}">
  {{ content }}

Note: the paragraph entity doesn't know its position in the field item list.

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.