Add file_uri filter

Created on 20 November 2019, over 5 years ago
Updated 14 January 2025, 3 months ago

First, a big thank you for this terrific module!

Problem/Motivation

Since #2924925: File url from media entity β†’ , file_url works with Media entities, however it is still a hurdle to use image_style with those.

image_style will not work with the output of file_url, it needs the file URI ("public://...") that is hidden in a place like node.field_media[0].entity.field_media_image.entity.uri.value

This works:
{{ node.field_media|file_url }}

While we cannot do this:
{{ node.field_media|image_style('thumbnail') }}

We have to resort to uglier code like:
{{ node.field_media[0].entity.field_media_image.entity.uri.value|image_style('thumbnail') }}

A concrete use case: to implement a theme from Drupal-agnostic UI patterns (eg. Pattern Lab), one needs to be able to pass on the URL of images at specific dimensions into the Pattern Lab Twig templates.

Proposed resolution

Have image_style use the same Media entity resolution mechanism than file_url

✨ Feature request
Status

Fixed

Version

3.0

Component

Code

Created by

πŸ‡¨πŸ‡­Switzerland mdupont Switzerland

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡¨πŸ‡­Switzerland sir_squall

    in Drupal 11, I'm using in the node.html.twig this code:

     {{ dump(node.field_image|file_uri) }}
    

    And I have two content rendered, one with a default image value setup in the content type, and the result is null and if I upload manually an image I got the uri, do you know if I need to do something?

    Thanks
    Alcindo

  • πŸ‡·πŸ‡ΊRussia Chi

    The logic for loading default image is in the image formatter. You may invoke it like follows node.field_image|view.
    However, if you only need a raw image URI you have to use preprocess hook to fetch default image from field storage settings.

  • πŸ‡¨πŸ‡­Switzerland sir_squall

    mmmhh ok, maybe you could help me, I have updated the code to that:
    {{ node.field_image|view|add_class("logo") }}

    But the class is not showing, and in the image.html.twig i have the standard code:
    <img{{ attributes }} />

    I wanted to retrieve the uri, like that I could create my self the

    I don't know if you could help

  • πŸ‡ΊπŸ‡ΈUnited States jwineichen

    I'm having some difficulty getting the streamlined file_url filter to work with image_style correctly. Not sure if it's a bug in the code or user error.

    When I use this:
    {{ node.field_media_image[0].entity.field_media_image.entity.uri.value|image_style('thumbnail') }}
    ...it correctly generates this:
    /sites/www.mydomain.org/files/styles/thumbnail/public/2025-01/image.jpg

    But when I use this:
    {{ node.field_media_image|file_url|image_style('thumbnail') }}
    ...it adds an extra /sites/www.mydomain.org/files into the middle of the url:
    /sites/www.mydomain.org/files/styles/thumbnail/public/sites/www.mydomain.org/fi...

Production build 0.71.5 2024