0

In views I want to rewrite output of image field by using twig function drupal_image (see module Twig Tweak). When I provide the image path directly, this code works:

{{ drupal_image('public://myimage.jpg' , image_responsive_style, responsive=true) }}

But I can not manage to provide image path by a variable, like this pseudocode:

{{ drupal_image('{{ uri }}' , image_responsive_style, responsive=true) }}

The problem seems to be how to provide the variable {{ uri }} properly inside the surrounding twig tag.

6
  • 2
    Remove the quotes arounded; otherwise, you are litteraly passing it as a string rather than a variable.
    – No Sssweat
    Commented May 4, 2019 at 20:42
  • Removing the quotes doesn't work neither.
    – Madam Adam
    Commented May 4, 2019 at 23:51
  • 1
    also forgot to mention remove the wiggly brackets around uri, {{ drupal_image(uri, image_responsive_style, responsive=true) }}
    – No Sssweat
    Commented May 5, 2019 at 0:34
  • What variable is uri? In an image field there is no such variable. You could use field_image__target_id though, because the twig function also works with a file id.
    – 4uk4
    Commented May 6, 2019 at 8:26
  • @NoSssweat: Your code in the above comments worked, and you were the first one who answered, but another user was the first who made an answer of it – so how should I vote?
    – Madam Adam
    Commented May 6, 2019 at 16:03

1 Answer 1

4
  1. Remove quotes.
  2. Remove wiggly brackets around.
  3. Surround image_responsive_style by quote.
  4. responsive=true Should passed as object:

    {{ drupal_image(uri , 'image_responsive_style', {responsive: true}) }}

2
  • I'm not sure about point 4. Twig seems to support named parameters. Otherwise this example from the cheat sheet of Twig Tweak: {{ drupal_image('public://ocean.jpg', 'wide', responsive=true) }} wouldn't work, because responsive is a boolean in fourth position.
    – 4uk4
    Commented May 6, 2019 at 8:24
  • yes i agree, you can pass responsive=true as boolean in the fourth position or as an attribute in the third, i have tested both but the first one doesn't work {{ drupal_image(uri , 'medium', {}, responsive=true) }} this doesn't work for me.
    – berramou
    Commented May 6, 2019 at 9:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.