Score:0

Accessing a Paragraphs field's from inside the parent paragraph

us flag

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?

4uk4 avatar
cn flag
*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`
sonfd avatar
in flag
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. :)
Score:0
cn flag

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).

William Breindel avatar
us flag
This is what I needed and needed to know. Thank you so very much. works like a charm.
cn flag
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. :)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.