1

I'm having trouble trying to access and print the values manually within the Paragraph. This is a snippet from the paragraph field template I have created. I wanted to access and print values manually instead of Drupal generating the markup using {{ slides.content }} so that I could customize the markup myself

{% for slides in items %}
    {{ slides.content['#options'].entity.field_label.value }}
{% endfor %}
  • 1
    Customize the markup by providing twig templates for the fields within the paragraph. Then no complex loops are really needed. – Kevin Apr 1 at 1:29
  • Aside from that is there anyway to access it directly instead of having to create multiple field twig templates? – clestcruz Apr 1 at 1:31
  • 1
    This looks like the standard loop of a field template, only that item was renamed to slides. Then slides.content contains the output of a field formatter configured by you in Manage Display of the paragraph type. – 4k4 Apr 1 at 6:31
2

Here's what worked for me to manually get parts of a field in a paragraph template.

This is in a field template for the actual paragraph, so for me field--field-paragraph.html.twig. I have an image field with the machine name field_image. This prints just the alt text of that image.

{% for item in items %}
  {{ item.content['#paragraph'].field_image.0.alt }}
{% endfor %}

I looked here for working with paragraph templates: https://www.drupal.org/project/paragraphs/issues/2662766#comment-12528934 and here for general drupal 8 field-getting: https://blog.usejournal.com/getting-drupal-8-field-values-in-twig-22b80cb609bd

1

I'd recommend not to use field templates for things like this, but use an entity template (like paragraph--BUNDLETYPE.html.twig) instead.

In an entity template you can access the rendered HTML output of any sub-field like {{ content.field_whatever }}, but you can also access raw field values for stuff like {% if paragraph.field_whatever.value == 'foo' %}

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.