Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

in this tuto, I'll show you how getting Drupal 8 Field image in Twig.

Print unlimited image field in node.html.twig

{% for key, item in node.field_image.value %}
  <img src="{{ file_url(content.field_image[key]['#item'].entity.uri.value) }}"
       alt="{{ item.alt }}" />
{% endfor %}

print one image field

in node.html.twig:

<img  src="{{ file_url(content.field_image['#items'].entity.uri.value) }}" alt="{{ node.field_image.alt }}" />
//OR
<img src="{{ file_url(content.field_image['#items'][0].entity.uri.value) }}" alt="{{ node.field_image.alt }}" />

in paragraph.html.twig:

<img  src="{{ file_url(content.field_image['#items'].entity.uri.value) }}" alt="{{ paragraph.field_image.alt }}" />
// OR
<img src="{{ file_url(content.field_image['#items'][0].entity.uri.value) }}" alt="{{ paragraph.field_image.alt }}" />

using https://www.drupal.org/project/twig_field_value module:

<img src="{{ file_url(content.field_image|field_target_entity.uri.value) }}" alt="{{ content.field_image|field_raw('alt') }}" />

to get image attributes use:

Alt: {{ node.field_name.alt }}

Title: {{ node.field_name.title }}

Height: {{ node.field_name.height }}

Width: {{ node.field_name.width }}

 

 

 

Comments

Add new comment

Restricted HTML

Search

Page Facebook

Become a patron

If you need some help or you search a Drupal freelancer don't hesitate to contact us.

 

Contact Us

All the content is FREE but I still need your help

 

Become a patreon