Skip to main content
  • Guides & Documentation

Knowledgebase

Featured articles, how-to guides and quick tips.

Get Media Image URL in Node Twig Template - Drupal 8

Sometimes it may be useful to access the raw URI or file URL of an image uploaded using a Media reference field in Drupal 8.

You may require the URL for the image file rather than wanting Drupal to render the image and surrounding HTML markup with it.

To accomplish this in a node template using Twig, simply use the following syntax, replacing the field name with your custom Media field machine name:

{% if node.field_custom_image is not empty %}
  {{ file_url(node.field_custom_image.entity.field_media_image.entity.fileuri) }}
{% endif %}