Skip to main content
added 316 characters in body
Source Link
4uk4
  • 101.7k
  • 7
  • 173
  • 217

The variable node is only preloaded in the node template (and the page template, if the url contains the node).

To answer your question, how Now if you want to access the node insidefields in a field template and to get the image url, you have to look in a different place:

field.html.twig:

{{ file_url(element['#object'].field_image.0.entity.uri.value) }}

I think it would beelement['#object'] is the parent entity in a field template and you can access any field from this entity (in your case the node).

If you want to access raw values from the actual field, it's better to follow the logic of the field twig and access the value inside of the items loop directly from the field item object #item:

{% for item in items %}
  {{ file_url(item.content['#item'].entity.uri.value) }}
{% endfor %}
 

Edit: Get the url of an image style

Install the module Twig Tweak and you can use the uri to get the url of an image style:

{% for item in items %}
  {{ item.content['#item'].entity.uri.value | image_style('thumbnail') }}
{% endfor %}

The variable node is only preloaded in the node template (and the page template, if the url contains the node).

To answer your question, how to access the node inside a field template and to get the image url, you have to look in a different place:

{{ file_url(element['#object'].field_image.0.entity.uri.value) }}

I think it would be better to follow the logic of the field twig and access the value inside the items loop:

{{ file_url(item.content['#item'].entity.uri.value) }}

Edit: Get the url of an image style

Install the module Twig Tweak and you can use the uri to get the url of an image style:

{{ item.content['#item'].entity.uri.value | image_style('thumbnail') }}

The variable node is only preloaded in the node template (and the page template, if the url contains the node). Now if you want to access node fields in a field template you have to look in a different place:

field.html.twig:

{{ file_url(element['#object'].field_image.0.entity.uri.value) }}

element['#object'] is the parent entity in a field template and you can access any field from this entity (in your case the node).

If you want to access raw values from the actual field, it's better to follow the logic of the field twig and access the value inside of the items loop directly from the field item object #item:

{% for item in items %}
  {{ file_url(item.content['#item'].entity.uri.value) }}
{% endfor %}
 

Edit: Get the url of an image style

Install the module Twig Tweak and you can use the uri to get the url of an image style:

{% for item in items %}
  {{ item.content['#item'].entity.uri.value | image_style('thumbnail') }}
{% endfor %}
added 272 characters in body
Source Link
4uk4
  • 101.7k
  • 7
  • 173
  • 217

The variable node is only preloaded in the node template (and the page template, if the url contains the node).

To answer your question, how to access the node inside a field template and to get the image url, you have to look in a different place:

{{ file_url(element['#object'].field_image.0.entity.uri.value) }}

I think it would be better to follow the logic of the field twig and access the value inside the items loop:

{{ file_url(item.content['#item'].entity.uri.value) }}

Edit: Get the url of an image style

Install the module Twig Tweak and you can use the uri to get the url of an image style:

{{ item.content['#item'].entity.uri.value | image_style('thumbnail') }}

The variable node is only preloaded in the node template (and the page template, if the url contains the node).

To answer your question, how to access the node inside a field template and to get the image url, you have to look in a different place:

{{ file_url(element['#object'].field_image.0.entity.uri.value) }}

I think it would be better to follow the logic of the field twig and access the value inside the items loop:

{{ file_url(item.content['#item'].entity.uri.value) }}

The variable node is only preloaded in the node template (and the page template, if the url contains the node).

To answer your question, how to access the node inside a field template and to get the image url, you have to look in a different place:

{{ file_url(element['#object'].field_image.0.entity.uri.value) }}

I think it would be better to follow the logic of the field twig and access the value inside the items loop:

{{ file_url(item.content['#item'].entity.uri.value) }}

Edit: Get the url of an image style

Install the module Twig Tweak and you can use the uri to get the url of an image style:

{{ item.content['#item'].entity.uri.value | image_style('thumbnail') }}
Source Link
4uk4
  • 101.7k
  • 7
  • 173
  • 217

The variable node is only preloaded in the node template (and the page template, if the url contains the node).

To answer your question, how to access the node inside a field template and to get the image url, you have to look in a different place:

{{ file_url(element['#object'].field_image.0.entity.uri.value) }}

I think it would be better to follow the logic of the field twig and access the value inside the items loop:

{{ file_url(item.content['#item'].entity.uri.value) }}