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


@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 }}

Create a template file for your paragraph.

paragraph__[view_mode] (e.g. paragraph--default.html.twig)

paragraph__[type] (e.g. paragraph--image.html.twig)

paragraph__[type]__[view_mode] (e.g. paragraph--image--default.html.twig)

source: Theming in Paragraphs for Drupal 8

In paragraph--[type].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.

@turpentyne 2019-05-23 00:20:38

@NoSssweat Thanks for referring me here... I'm trying to do something similar to above, But my paragraph template isn't overriding. My field in a content type is called "field_legal_section", referencing a paragraph type called "legal_section"... so I created "paragraph--field-legal-section.html.twig" and dropped {{ }} & {{ content.field_section_title }}. But content type template is just rendering default values for the token in my content type template ( {{ content.field_legal_section }} ) . What might I be missing?

@No Sssweat 2019-05-23 01:06:52

@turpentyne Did you place it in /themes/your_theme/templates folder? Did you clear the cache?

@turpentyne 2019-05-23 01:53:23

@NoSssweat I've done both - placed in the template folder of my theme and cleared the cache.

@turpentyne 2019-05-23 02:01:12

@NoSssweat update... I figured out the main problem.. Your example above looks like it's naming the template for the paragraph field within the content type. That's what I assumed was needed. When I change the template to my paragraph type's name (which is slightly different) it works - although it's not showing the field ID, which is my primary goal.

@No Sssweat 2019-05-23 17:32:27

@turpentyne Good catch! What exactly are you referring to when you say field ID? the machine name of the field? field_legal_section or ???

@turpentyne 2019-05-23 18:36:22

@NoSssweat Not the machine name, but a unique id... Maybe it's called something else? Basically, each paragraph entry ends up with a sequential number? I can see it when I pull the paragraph in a view using "Paragraph: ID" , so I presumed I could get it with just {{ }} ... but no luck.

@turpentyne 2019-05-23 18:47:46

@NoSssweat I just tried {{ }} and {{ }}.. it still didn't work. Maybe at this point I need to make a new question for this. I haven't found an answer that quite works yet

@No Sssweat 2019-05-23 19:57:33

@turpentyne you can get it with {{ elements['#paragraph'].id() }}

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

Related Questions

Sponsored Content

3 Answered Questions

[SOLVED] Entity print get variable in twig template

3 Answered Questions

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

  • 2017-03-18 04:58:18
  • Henry Tran
  • 8223 View
  • 4 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
  • 119 View
  • 0 Score
  • 1 Answer
  • Tags:   8 hooks paragraphs

2 Answered Questions

[SOLVED] How to load paragraphs fields in hook_form_alter to disallow access?

  • 2019-01-08 13:41:05
  • harshal
  • 404 View
  • 0 Score
  • 2 Answer
  • Tags:   8 hooks paragraphs

0 Answered Questions

How to print particular referenced paragraphs in twig?

2 Answered Questions

2 Answered Questions

[SOLVED] How can I print subparagraphs?

1 Answered Questions

2 Answered Questions

[SOLVED] twig to print paragraph field in controller

  • 2016-09-26 18:58:53
  • Brittany
  • 3075 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
  • 1994 View
  • 3 Score
  • 2 Answer
  • Tags:   theming 8

Sponsored Content