logo

Field Values Drupal 8

admin
28 Jun 2021

Node field twig template to display multiple images/item. We can theme/design each item for that field

I am using node template as follow,

node--mycontenttype.html.twig

For example my content type name is "banner" then template name as.

node--banner.html.twig

I added field as below and its display all images but Its not able to wrap each images with div so that you can add classes to create slider.

{{ content.field_gallery_images }} // field_gallery_images is field name

Then have to create template as below.

field--node--fieldname--contenttype.html.twig

For example my field name is "field_gallery_images" and content type name is "banner" then I can your template name as below.

field--node--field_gallery_images--banner.html.twig

Then I have put following code in that template.


{% set count = items | length %}
{% if count == 1 %}
	{% for item in items %}
		{{ item.content }} // Can add div and class as required
	{% endfor %}
{% else %}
	{% for item in items %}
		{{ item.content }} // Can add div and class as required
	{% endfor %}
{% endif %}

I have checked condition it has single image or multiple images but its not essential. In my case there was issue with single image so I have check condition. You can add code as below if you don't want to check condition


{% for item in items %}
	{{ item.content }} // Can add div and class as required
{% endfor %}