Skip to main content

Adding attributes to the <img> tag of an image field in Drupal 8

While theming an image field in Drupal 8, you might have a need to add additional html attributes to the <img /> tag itself. We've found the following snippet will allow you to add these attributes within your field.html.twig file:

  1. {% for item in items %}
  2. {{ item.content|merge({'#item_attributes': {'uk-cover': ['attribute value here']}}) }}
  3. {% endfor %}

 


Comments