Skip to content

Instantly share code, notes, and snippets.

@thamas
Created April 16, 2019 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thamas/5fbcf46bacfc0eb9387234f0cc232dde to your computer and use it in GitHub Desktop.
Save thamas/5fbcf46bacfc0eb9387234f0cc232dde to your computer and use it in GitHub Desktop.
Get Drupal (media) image in Twig template
{#
Source: https://youtu.be/jNqXZ4Jb2No?t=822
DrupalCon Seattle 2019: Pattern Lab: The Definitive How-to
#}
{% set img = node.field_img.entity.getFileUri() %}
{# result: public://images/ocean.jpg #}
{% set imgUrl = file_url(img) %}
{# result: /sites/default/files/ocean.jpg #}
{# requires Twig Tweak Drupal module #}
{% set smallImgUrl = img | image_style('small') %}
{# result: /sites/default/files/styles/small/ocean.jpg #}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment