Iterate through multiple value content field in Twig template


You can fix this in a field twig. There you can use the existing loop to iterate over the field items:


{{ content.field_admin_tags }}


<ul{{ attributes }}>
  {% for item in items %}
    <li>{{ item.content }}</li>
  {% endfor %}

This example replaces <div> with <ul>. Don't remove {{ attributes }} or bypass the field template, see What can break quickedit, and how do I fix it?

Agree with 4k4 the field template is the best place, if you really want it in the entity template (e.g. node) you can do something like this:

{% for key, item in content.field_tags if key|first != '#' %}
  <div class="item-{{ key + 1 }}">{{ item }}</div>
{% endfor %}

But honestly I think it's a bit ugly, the field template is the right place.

If you, like me, are looking for a way to iterate through paragraphs on a node's twig template, here is how to do it:

Suppose you have a node with a multivalued paragraph field, so a content editor can create multiple paragraphs and you wish to iterate through each paragraph on the nodes twig template (for example to add a wrapper around each paragraph):

{% for key,value in node.my_paragraph_field.value %}
    {{ content.my_paragraph_field[key] }}
{% endfor %}

UPDATE: I needed to find another way to print all paragraphs without using content. The content variable contains everything you set up in the 'Manage display' section of the node, but my current work method is to never use 'Manage display' nor layouts since you can actually access all data in the node twig file, and almost all settings you would do on 'Manage display', like applying an image style, or setting a date format, ... you can do directly in twig.

For me this is an advantage because I know that everything I see comes from the twig file, and I do not need to look for some obscure field settings that may add classes somewhere. So everything I see comes from just one place (the nodes twig file) and not a combination of the twig file and the manage display screen.

Anyway, using the awesome Twig Tweak module, here is how to print a multi value paragraph field in a node's twig file without having to use the content variable:

{% for item in node.field_paragraphs %}
    {{ drupal_entity('paragraph', item.target_id) }}
{% endfor %}
Tags: Theming / Drupal 8

Similar questions

JSON through Services View- Authenticating through Parameters when accessing JSON through external app
My Drupal site is generating JSON through Views. I am using Services for that. It is something like this:- Now the problem is that I want to restrict anyone from writing that URL in the any browser and retrieving all data. Do you think there is a way to authenticate it? Probably, in my external app, when I am accessing the data, I can add parameter...
How to iterate over all the nodes from a content type
I want to recover some info from a field of all the nodes of a specific content type. I know how to do it using a db_query and getting all the node ID then iterate over them. What I would like to know is if there's is another way around this using drupal functions.
How do I iterate over values of a multivalued field with entity_metadata_wrapper?
For instance, I have a taxonomy_term_reference field with cardinality -1. If one or more terms are selected, I can use this code: But if the field has zero values selected, this code throws: This happens in a preview form step while creating a custom entity. The entity has not yet been saved but I am showing a preview of the properties and field da...
How to iterate over all field collection items in an eMail body using Rules?
I'm trying to write a rule (using Rules) that will send an email to users of a role. Within the body of the email, I'd like to display the contents of a nested field collection. I've tried using what Rules gives me for data selectors, but I can't seem to put it together. Specifically, I have two field collections: field-boat-lineup and field-equip-...
Define custom twig page template for specific multiple node pages by nid - page--custom.html.twig
I have a page from different content type and I want to define a custom twig page template to these pages by their nid. Eg. I have pages like node/2, node/4, node/20 and node/40 from the different content type and I want to apply page--custom.html.twig template only these specific pages by checking their ID. Anyone know how to do this?
Get a views result using PHP code, and iterate the proper way
I have a view called all_projects_list. This view fetches Fields (not nodes), and i'd like to get it by code, and iterate on it. I managed to do this by using: Which as you can see is very, very dirty. Is there a Drupal way of doing this ? Thanks in advance.

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.