By usernameabc


2017-09-29 17:38:00 8 Comments

I have a content type, basic, where i have added a Paragraph field (field_pr) that has two fields: field_text, field_color. I have a twig template node--basic--full.html.twig where i want to print the value of the paragraph field (node.field_pr) because it can be more than 1 value. The number of paragraph that can be added is limited to 10 so think of node.field_pr having up to 10 values.

When i print the array for content.field_pr i get a huge array, but i only want the values from the field_pr.field_text and field_pr.field_color. How would i be able to do this from the node twig template? I am not able to get it done.

I have tried the following but i cannot seem to access the values. What am I missing?

{% for field in content.field_pr %}
   {{ field[loop.index0].entity.field_text.value }}
   {{ field[loop.index0].entity.field_color.value }}
{% endfor %}

{% for field in content.field_pr %}
   {{ field[loop.index0]['#paragraph'].field_text.value }}
   {{ field[loop.index0]['#paragraph'].field_color.value }}
{% endfor %}

Here is an image of the array that is printed for the node.field_pr field. enter image description here

6 comments

@youssef simon 2018-11-06 20:17:11

This work for me

{% for item in node.field_table %}
  {{ item.entity.field_period_reco.value }}
  {{ item.entity.field_saham.value }}
{% endfor %}

@Hashmat 2018-11-07 10:53:20

To print paragraph fields in a loop (for sliders, etc.) in page--content-type.html.twig use below snippet:

{% for item in node.field_paragraph_mac_name %}
  {{ item.entity.field_title.value }}
  <img src="{{ file_url(item.entity.field_image.entity.fileuri) }}" />
{% endfor %}

@Hashmat 2018-11-09 10:31:19

To print alt tag, use alt="{{item.entity.field_image.alt}}".

@Mocarbgoode 2018-10-31 23:59:22

In twig templates where I am pulling in content from an array of fields, I have found this works: {{ content.field_paragraphs.0 }} using 0 for the first item and going from there.

@usernameabc 2018-11-06 23:30:24

this may fail if you allow for more then one Paragraph item added.

@AdamB 2018-10-17 14:59:48

Building on the answer by user33560. If you have have nested paragraphs and you want to print or get the 2nd-level paragraph fields from within the 1st-level paragraph.html.twig:

{% set paragraph_name = content.field_paragraph_name %}

{% for item in paragraph_name['#items'] %}
    {{ drupal_entity('paragraph', item.target_id) }}
{% endfor %}

You can also use {{ content.field_paragraph_name }} to print all paragraph items, or print a subset such as {{ content.field_paragraph_name.field_example }}. Use {{ content.field_paragraph_name|without('field_example') }} to temporarily suppress the printing of a given child element.

@user33560 2018-02-06 10:52:11

I figured this one out. With Twig Tweak enabled you can print paragraphs from the node object without using content:

{% for item in node.field_my_paragraph_field %}
     {{ drupal_entity('paragraph', item.target_id) }}
{% endfor %}

@Hudri 2018-11-07 11:11:19

Just use {{ content.field_my_paragraph }} instead, does exactly the same, is cache safe, is more performant, and does not require an extra module.

@usernameabc 2018-11-07 19:36:33

@user33560 if you follow the accepted answer, you will not need an extra module.

@No Sssweat 2017-09-30 08:37:38

Not sure if it's possible with your current approach. Here is how I would approach this.

In your node template file just do a {{ content.field_pr }} and then create a template file for your paragraph. So it would be paragraph--field-pr.html.twig

In paragraph--field-pr.html.twig you do {{ content.field_text }} and {{ content.field_color }}

@clestcruz 2018-06-08 07:24:35

Apologies for my ignorance but how do I render paragraph.html.twig on my template file (page.html.twig)?

@No Sssweat 2018-06-08 07:26:40

@clestcruz why not in node.html.twig?

@clestcruz 2018-06-08 07:30:29

@NoSssweat node.html.twig? Sorry I was trying to figure out how I can just print the values from my paragraph to my templates file. So far I've managed to create a paragraph.html.twig. I'm still new to Drupal apologies

@No Sssweat 2018-06-08 07:32:59

@clestcruz, yes you'll need to create node--[content-type-name].html.twig. Ex: node--articles.html.twig and follow my answer.

@clestcruz 2018-06-08 07:34:21

@NoSssweat Not sure if this is the same or different but what I did was created a page--articles.html.twig

@No Sssweat 2018-06-08 07:38:39

@clestcruz node is inside page. More specifically the {{ page.content }} part outputs the node twig. You're better off using node because page contains the entire page structure. Page It's not meant to be used for printing fields.

@clestcruz 2018-06-08 07:42:52

@NoSssweat Hmm well this is new to me. Most of my content type in drupal are using page--name-of-content-type.html.twig and not node.html.twig to render the values from the fields. So you're telling the correct way to print the fields is to use node.html.twig instead?

@No Sssweat 2018-06-08 08:01:10

@clestcruz yes exactly, the drupal way is to use node.htm.twig to print node fields. If you have a block, you use block.html.twig.

@clestcruz 2018-06-08 09:03:16

@NoSssweat ok, but how do I fetch or print the values from the paragraphs to my node.html.twig I tried calling it like a partial template {% include '@iom/layout/paragraphs/paragraph--multimedia.html.twig' %}

@No Sssweat 2018-06-08 09:12:17

@clestcruz read my answer. First you print the paragraph field in node.html.twig then create a paragraph template file (if you want).

@No Sssweat 2018-06-08 09:20:52

@clestcruz if you haven't yet, you need to go to your content type's manage fields and a paragraph field. Then you just simply print this field in node.html.twig just like any other field, nothing special. BTW no need to {% include %} any template.

@usernameabc 2018-11-06 23:29:12

@clestcruz i recommend using what NoSssweat said. Use node.html.twig and display the field using {{content.field_machine_name_paragraph}}. Then enable Twig debugging and you can create a new twig template from there.

Related Questions

Sponsored Content

3 Answered Questions

[SOLVED] How to print alt or title field image in twig node template?

  • 2017-03-18 04:58:18
  • Henry Tran
  • 7563 View
  • 5 Score
  • 3 Answer
  • Tags:   theming 8

1 Answered Questions

[SOLVED] Pass hidden fields using drupalSettings to Twig template

  • 2019-02-20 00:17:55
  • usernameabc
  • 58 View
  • 0 Score
  • 1 Answer
  • Tags:   8 hooks paragraphs

1 Answered Questions

[SOLVED] How to append an HTML tag to a field?

1 Answered Questions

[SOLVED] How to print/render nested Paragraph values to twig template?

2 Answered Questions

2 Answered Questions

[SOLVED] How can I print subparagraphs?

1 Answered Questions

[SOLVED] Using twig|without for a commerce product

  • 2017-11-13 17:04:42
  • Patrick Kenny
  • 302 View
  • 1 Score
  • 1 Answer
  • Tags:   8 drupal-commerce

1 Answered Questions

2 Answered Questions

[SOLVED] twig to print paragraph field in controller

  • 2016-09-26 18:58:53
  • Brittany
  • 2813 View
  • 2 Score
  • 2 Answer
  • Tags:   theming 8

2 Answered Questions

[SOLVED] How to get media entity field value in twig?

  • 2017-04-20 10:25:23
  • Philipp Michael
  • 1850 View
  • 4 Score
  • 2 Answer
  • Tags:   theming 8

Sponsored Content