Closed (fixed)
Project:
Twig Tweak
Version:
8.x-2.4
Component:
Code
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
20 Nov 2017 at 12:30 UTC
Updated:
28 Jul 2019 at 15:49 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
leon kessler commentedAttached a patch for this.
A couple of notes:
Comment #3
Ralf Eisler commented{{ media_file_url(node.field_media.entity) | image_style('thumbnail') }}That would be excellent and would exactly match my use-case.
Comment #4
rockaholiciam commentedCant seem to make it work with Drupal 8.4. Could the fact that media is now part of core be the culprit?
Comment #5
chi commentedSeems 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.
Comment #6
leon kessler commentedSorry, 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)....
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
Comment #7
chi commentedWell, 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.
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 likeuri.Comment #8
leon kessler commentedIsn'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 :-)
Comment #9
chi commentedSorry, I meant File entity.
To clarify things below is a quick schema of obtaining file URI from a node.
Comment #10
leon kessler commentedOkay 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).Comment #11
chi commentedComment #12
seanbOne 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.
Comment #13
etroid commentedI 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.
Cheers
Comment #14
leon kessler commented@Etroid
You added twig filters, not functions. So the usage should be:
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.
Comment #15
chi commentedTwig Tweak just got a
file_urlfilter which supports both file fields and media fields.https://git.drupalcode.org/project/twig_tweak/blob/6b1ae2d7ccd115b5d82c0...
Testing and feedback would be appreciated.
Comment #16
chi commentedFixed in Twig Tweak 2.4.
https://www.drupal.org/project/twig_tweak/releases/8.x-2.4