0

In page.html.twig I want to show a paragraph reference field which contains a left and right text_field.

The name of the reference field in the node is: field_intro. That field is linked to a paragraph type named: text_left_right

Using below code I can render the paragraphs, but if I add an extra field to the paragraph type, it is not updated. I would like to render all fields, even if I add a new field in the future.

That is not going to happen with this piece of code:

{% for item in node.field_intro %}
  {{ item.entity.field_left.value }}
{% endfor %}

I found a lot of answers for rendering in node.html.twig but not in page.html.twig.

UPDATE after below comments and answer: It is not recommended to print fields in a page.html.twig page. Better use views or blocks to establish what you need. And if you want to use templates, use node.html.twig.

9
  • 1
    Fields are meant to be inside of node.html.twig. If you want to render them in page.html then you're going out of the content area. If I was you, I either would use a Views block to display the paragraphs or create a block type and add the paragraphs there. – No Sssweat Jan 17 '20 at 10:50
  • Do you mean adding a new field value to the same existing field (cardinality) or really adding a completely new field with a new field name? – Hudri Jan 17 '20 at 10:54
  • @no-sssweat: I thought because the 'node' is available it is ok to use it in the page template? – Lance Jan 17 '20 at 12:42
  • @Hudri: adding a new field in the paragraph type text_left_right – Lance Jan 17 '20 at 12:43
  • 1
    I advise to use the render pipeline instead of print directly. You can create a view block to render a node in a particular view mode. – Kevin Jan 17 '20 at 13:00
0

but if I add an extra field to the paragraph type, it is not updated. I would like to render all fields, even if I add a new field in the future.

To answer the question, you just need to loop though the paragraph fields.

{% for item in node.field_intro %}
  {% for paragraph_field in item.entity %}
     {% if 'field_' in paragraph_field %}
       {{ paragraph_field.value }}
     {% endif %}
  {% endfor %}
{% endfor %}
2
  • {{ item.entity.paragraph_field.value }} must be {{ paragraph_field.value }} I guess? And this is also printing other fields like ID, revision_id, ... – Lance Jan 17 '20 at 13:45
  • @Lance I have fixed it. – No Sssweat Jan 18 '20 at 3:13

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.