0

I have a paragraph twig file that I'm adding to my subtheme.

Inside of it I have another paragraph that is being used. And there could be more than 1 of the child paragraph.

I have this code:

 {% for key, item in content.field_faq_section_question if key|first != '#' %}
  {{ item }}
    
{% endfor %}

And it prints the title and the text fields of the paragraph properly. But What I need to do it access each individually here.

I have tried a lot of different ways to access the fields and none work. A dump and a var_dump of item ends with a blank screen.

I could create another twig file for this paragraph, and it works, but I'm hoping to use the key to add an id to a wrapper div.

I'd like to:

 {% for key, item in content.field_faq_section_question if key|first != '#' %}
  {{ item.title }}
    {{ item.text }}
{% endfor %}

What is the best way of doing this?

2
  • To use the key to add an id to a wrapper div you need a field template, not a paragraph template: field--paragraph--field-faq-section-question.html.twig
    – 4uk4
    Commented Mar 21, 2022 at 21:37
  • You should be careful doing stuff like this. If you render things like this then you don’t render the whole field, possibly skipping important cache metadata or attributes, etc that may have been added by modules or themes. Generally speaking, you’re better off not fighting Drupal’s systems to “not create another file” or some similar reason. Fighting these systems will catch up with you eventually, I promise. :)
    – sonfd
    Commented Mar 22, 2022 at 2:31

1 Answer 1

1

First, I'd recommend https://www.drupal.org/project/twig_xdebug to help you debug inside of twig templates. It'll make your life 100 times easier.

Your issue is that content.field_faq_section_question is a render array, so looping over its "items" is not actually looping over the referenced entities -- it's looping over the render array elements. That's why you've needed to add that annoying check for whether the key starts with #.

If you want to loop over the value of the reference field, then you need to loop over the #items. Then you need to grab the actual paragraph that is being referenced in order to access its fields, which can be done with [some_entity_reference_item].entity.

{% for k, paragraph_reference in content.field_faq_section_question['#items'] %}
  {% set paragraph = paragraph_reference.entity %}
  {% set my_id = paragraph.field_some_text_field.0.value %}

  <div id="{{ my_id }}">
    // ... whatever you want to render
    // I think this'll work the same way as the {{ item }} you're using already
    {{ content.field_faq_section_question[k] }}
  </div>
{% endfor %}

That said, depending on what you're doing, you may want to use a field template as mentioned by 4uk4, or actually leverage another paragraph template (like you're avoiding).

2
  • This is what I needed and needed to know. Thank you so very much. works like a charm. Commented Mar 22, 2022 at 1:09
  • Great! Please accept this answer when you're able to so that it doesn't get brought back to the front page by the SE bot for the rest of time. :)
    – Mrweiner
    Commented Mar 22, 2022 at 3:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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