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.

  • 2
    Remove the quotes arounded; otherwise, you are litteraly passing it as a string rather than a variable. – No Sssweat May 4 at 20:42
  • Removing the quotes doesn't work neither. – Madam Adam May 4 at 23:51
  • 1
    also forgot to mention remove the wiggly brackets around uri, {{ drupal_image(uri, image_responsive_style, responsive=true) }} – No Sssweat May 5 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. – 4k4 May 6 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 May 6 at 16:03
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}) }}

  • 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. – 4k4 May 6 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 May 6 at 9:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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