0

I have a content type as "worker". I have a Paragraph "test" and I am referencing "test" paragraph inside my content type "worker". This allows multiple values to be saved.

How can I get the multiple "test" paragraph's field_name inside my node--worker.html.twig file directly?

I am able to get the single value of that field like below:

{{ node.field_test.entity.field_cdsfasdf.value }}

But, unable to fetch the multiple values for this field inside my node--worker.html.twig. I tried using for loop to fetch the multiple values like the code below:

{% for key, item in node.field_para %}
    {{ key }}
    <div class="item"> {{ item.entity.field_cdsfasdf.value }} </div>
{% endfor %}

But, I am facing the following error:

Exception: Object of type Drupal\entity_reference_revisions\Plugin\Field\FieldType\EntityReferenceRevisionsItem cannot be printed. in Drupal\Core\Template\TwigExtension->escapeFilter() (line 465 of DRUPAL_ROOT\exp\core\lib\Drupal\Core\Template\TwigExtension.php).

Exception: Object of type Drupal\node\Entity\Node cannot be printed. in Drupal\Core\Template\TwigExtension->escapeFilter() (line 465 of DRUPAL_ROOT\exp\core\lib\Drupal\Core\Template\TwigExtension.php).
0

Off the top of my head...

For multiple value fields you either render out the paragraph field itself:

{{ content.field_paragraph_field_name }}

and let Drupal handle the multiple value rendering, or do your own loop:

{% for item in content.field_paragraph_field_name %}
  {{ item.your.array.keys.here.value }}
{% endfor %}

Try:

{% for item in content.field_paragraph_field_name %}
  {{ dump(item | keys) }}
{% endfor %}

to see what keys you need to follow to your value.


I do recommend doing this kind of theming in a paragraph--paragraph-name.html.twig file, and then just printing the field in node.html.twig.

For a paragraph template file, it would look something like:

{% for item in content.field_paragraph_field_name['#items'] %}
  <div>{{ item.value }}</div>
{% endfor %}

For the error, looks like you're cycling through the reference field, but you need to cycle through the values of a field inside the reference field. (I'm sure someone could explain this better, but entity holds the field values you want to loop)

Instead of:

{% for item in node.field_para %}
    {{ item.entity.field_cdsfasdf.value }}
{% endfor %}

try something more like:

{% for item in node.field_para.entity.field_cdsfasdf %}
    {{ item.value }}
{% 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.