5

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

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

  • Apologies for my ignorance but how do I render paragraph.html.twig on my template file (page.html.twig)? – clestcruz Jun 8 '18 at 7:24
  • @clestcruz why not in node.html.twig? – No Sssweat Jun 8 '18 at 7:26
  • @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 – clestcruz Jun 8 '18 at 7:30
  • @clestcruz, yes you'll need to create node--[content-type-name].html.twig. Ex: node--articles.html.twig and follow my answer. – No Sssweat Jun 8 '18 at 7:32
  • 1
    @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. – usernameabc Nov 6 '18 at 23:29
2

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 %}
  • 1
    Just use {{ content.field_my_paragraph }} instead, does exactly the same, is cache safe, is more performant, and does not require an extra module. – Hudri Nov 7 '18 at 11:11
  • @user33560 if you follow the accepted answer, you will not need an extra module. – usernameabc Nov 7 '18 at 19:36
1

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 %}
  • To print alt tag, use alt="{{item.entity.field_image.alt}}". – Hashmat Nov 9 '18 at 10:31
0

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.

-1

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.

  • this may fail if you allow for more then one Paragraph item added. – usernameabc Nov 6 '18 at 23:30
-1

This work for me

{% for item in node.field_table %}
  {{ item.entity.field_period_reco.value }}
  {{ item.entity.field_saham.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.