A useful tool would be to extract the file url from a media entity.

So you would be able to do something like this:

{{ media_file_url(node.field_media.entity) }}

Which also means you could do something like this:
{{ media_file_url(node.field_media.entity) | image_style('thumbnail') }}

This is highlighted in #2887314: Image and Responsive Image Styles and also https://drupal.stackexchange.com/questions/194749/how-to-find-image-styl...

Comments

Leon Kessler created an issue. See original summary.

leon kessler’s picture

Status: Active » Needs review
StatusFileSize
new1.95 KB

Attached a patch for this.

A couple of notes:

  1. There's stuff currently moving around with the media_entity module and core. So it may be worth waiting until this has settled a bit more.
  2. The patch does not include tests, as this would create a dependency on the media_entity module (again it may be worth waiting to see where things end up).
Ralf Eisler’s picture

{{ media_file_url(node.field_media.entity) | image_style('thumbnail') }}
That would be excellent and would exactly match my use-case.

rockaholiciam’s picture

Cant seem to make it work with Drupal 8.4. Could the fact that media is now part of core be the culprit?

chi’s picture

Status: Needs review » Closed (won't fix)

Seems like extracting URI value from image/media field can be easily done with the following pattern.
{{ file_field.entity.uri.value }}

Also as #2848104: Support responsive images landed rendering images should became more easier.

Here are some useful snippets.

{# Render media using node field formatter. #}
{{ drupal_field('field_media', 'node', node.id) }}

{# Render an entire media entity. #}
{{ drupal_entity('media', node.field_media.entity.id) }}

{# Render image field. #}
{{ drupal_field('field_media_image', 'media', node.field_media.entity.id) }}

{# Render only image. #}
{{ drupal_image(node.field_media.entity.field_media_image.entity.id, 'thumbnail') }}

{# Render image URI. #}
{{ node.field_media.entity.field_media_image.entity.uri.value | image_style('thumbnail') }}
leon kessler’s picture

Status: Closed (won't fix) » Needs work

Sorry, re-opening, but setting to needs work - as rockaholiciam reported that it no longer works with the new core media module (which is what I was half-expecting)....

Seems like extracting URI value from image/media field can be easily done with the following pattern.
{{ file_field.entity.uri.value }}

If you do this on a media entity, you get the media url (which is for the media HTML page, e.g. http://www.example.com/media/123).
It's probably possible to do something like node.field_media_reference.entity.field_file.entity.uri.value (haven't tested this)

But the issues with that are a) You need to know both field names (media reference and file reference) and b) it's long and messy

chi’s picture

Well, besides Media entity type there is an Image entity type which is much more widely used. And it also lacks a method to extract a url from entity. From this point we need at least two such functions.

  1. drupal_image_file_uri()
  2. drupal_media_file_uri()

Overall both of these cases looks two specific. Maybe we should move this to Twig field value module which is by the way already has similar Twig filter field_target_entity.

Also wonder what is wrong with {{ drupal_field('field_media', 'node', node.id) }}? It seems so simple to configure a display mode and render the media without having to play with low level entity properties like uri.

leon kessler’s picture

Isn't an image entity just a bundle type of the media entity?

But yes it could be considered to move to twig field value module. But I personally think that module is more specific, does one very small task.
Twig tweak has a lot more varied functionality, which I appreciate makes it more difficult to maintain, but also makes it more suitable to support this use case.

Also, I don't think that editing a display mode can be the ultimate solution. Often projects will opt to use a twig-only theming methodology. It comes back to the clicking vs theming debate, and it's normally advisable to choose one over the other, rather than a bit of both.

But these are just my opinions :-)

chi’s picture

Isn't an image entity just a bundle type of the media entity?

Sorry, I meant File entity.

To clarify things below is a quick schema of obtaining file URI from a node.

// Image field.
Node -> [entity_reference] -> File -> [URI]
// Media field.
Node -> [entity_reference] -> Media -> [entity_reference] -> File -> [URI]
leon kessler’s picture

Okay yes, I suppose the same function should be available for file entities (although this ticket was specifically for media entities, as those are pretty troublesome to work with).

And yes I think you are right about moving to twig field value (I didn't know about field_target_entity).

chi’s picture

Project: Twig Tweak » Twig Field Value
Version: 8.x-2.x-dev » 8.x-1.x-dev
Status: Needs work » Active
seanb’s picture

One important thing to remember is not all Media contains a file field as a source. This makes it a little harder to write and use a generic method to fetch the source data.

etroid’s picture

I agree this would be useful to have as it's currently a paint to write in twig. Attached is a patch for both File and Media entities. If this is something we want to include in twig_field_value we'll have to write some tests for these new twig filters.

{{ field_media_file_uri(node.field_media_reference) }}
{{ field_file_uri(node.field_file_reference) }}

Cheers

leon kessler’s picture

@Etroid

You added twig filters, not functions. So the usage should be:

content.field_media_reference|field_media_file_uri

I needed to use this functionality again, and the patch I previously posted on #2 for twig_tweak no longer works (due to API changes).
I tried patch from #13, but couldn't get it working. I'm not sure if it is the patch or an issue with twig_field_value itself and our project.

Anyway, I decided to update the patch from #2. This again is for twig_tweak module and not twig_field_value. Apologies for any confusion. Posting here anyway in case anyone else wants it.

chi’s picture

Twig Tweak just got a file_url filter which supports both file fields and media fields.
https://git.drupalcode.org/project/twig_tweak/blob/6b1ae2d7ccd115b5d82c0...

Testing and feedback would be appreciated.

chi’s picture

Project: Twig Field Value » Twig Tweak
Version: 8.x-1.x-dev » 8.x-2.4
Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.