Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save krisrobinson/1279375c9742c751ad9b2ef31d5a7e58 to your computer and use it in GitHub Desktop.
Save krisrobinson/1279375c9742c751ad9b2ef31d5a7e58 to your computer and use it in GitHub Desktop.
Drupal 8 loop through and print paragraph fields in field.html.twig
<div{{ attributes }}>
{# First check if there is a section video #}
{% for item in items if item.content['#paragraph'].field_section_video is not null %}
{% if loop.first %}
<div{{ item.attributes }}>{{ item.content }}</div>
{% endif %}
{% else %}
{# If no video was found, check if there is a section_image #}
{% for item in items if item.content['#paragraph'].field_section_image is not null %}
{% if loop.first %}
<div{{ item.attributes }}>{{ item.content }}</div>
{% endif %}
{% else %}
{# Revert to default#}
<div {{ item.attributes }} class="no-media">No Media</div>
{% endfor %}
{% endfor %}
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment