Hi and thank you very much for this great module.
Has someone already used "field_target_entity" in combination with media?
In our case we have a content -> media field via entity reference -> media item and need to get the images uri like in this example:
Referenced entities
<img src={{ file_url(content.field_image|field_target_entity.uri.value) }} alt={{ content.field_image|field_raw('alt') }} />
but with a step in between, we think... we could not figure out how to do this yet.
Media will be used in several D8 installations so we thought it might be useful to discuss this and document the solution on the module page.
Any ideas?
Comments
Comment #2
thomas.frobieterThe solution is:
{{ file_url(content.field_node_image|field_target_entity.field_media_image.entity.uri.value) }}where:
field_node_image is the entity reference field in the content targeting a media entity
field_media_image is the image field within the media entity
Any better solutions?
The worst problem was that the variables on the path were not shown in kint so we had to guess them... that's horrible for Drupal 8... if someone should know why or how to do that better, please tell us ;) (unrelated to this issue)
Comment #3
leisurman commented@thomas.frobieter Thank you. I am using the default media bundle and its field machine name is [image] So this works in my node page template.
{{ file_url(content.field_global_image|field_target_entity.image.entity.uri.value) }}And if I want a background image I can do this.Comment #4
Ralf Eisler commentedWorks great with the default Media image bundle:
{{ file_url(content.field_my_image|field_target_entity.image.entity.uri.value) }}In addition, this returns an image-style:
{{ file_url(content.field_my_image|field_target_entity.image.entity.uri.value | image_style('my_image_style')) }}Comment #5
leon kessler commentedFWIW, I created this patch for the twig tweak module (as I didn't want my twig files to get littered with field names).
There could be a case for the patch to be moved to this module instead.
Comment #6
ruplIs there something special that has to be done for multi-value fields? I tried prefixing the ER field with a specific array index like so:
I'm doing this on a Drupal Commerce product page, so it's very possible that the situation is complicated by their data structure. Anyway I still thought it was worth asking for others who have multi-value entity references to Media Entities.
EDIT: the solution is to iterate on the referenced entity, NOT the primary field. Example:
Comment #7
drewid commentedThank you all for sharing this information. I've been trying to figure out how to get the URI of an entity referenced Media image for a couple of days and finally found a solution with help of the examples here. #4 worked for me after I changed the machine name to match the machine name of the image file in Media as it appears in #6.
So Instead of:
{{ file_url(content.field_my_image|field_target_entity.image.entity.uri.value) }}
I used:
{{ file_url(content.field_my_image|field_target_entity.field_media_image.entity.uri.value) }}
where field_my_image is the my paragraph bundle.
And this to get the alt.
{{ content.field_my_image|field_target_entity.field_media_image.alt }}
I'm using Drupal 8.6.3 and I don't think I changed the machine name for images in Media. Perhaps it was changed between versions, IDK.
Comment #8
adrian_s_m commentedThis conversation was really useful for me, thank you all. I had a paragraph block with 4 divs where I had images that were rendered as inline background image for some links in each block set as image field and this what I used to get them:
and then
<a href="{{ content.field_link_to_content[0]['#url'] }}" class="image-box" style="background-image: url('{{ image_url1 }}')">Comment #9
sutharsan commentedClosing the issue as it seems to be resolved.
Comment #10
thomas.frobieterWorth it to put a media example in the documentation?
I would consider {{ file_url(content.field_my_image|field_target_entity.image.entity.uri.value) }}
Comment #12
auxiliaryjoel commentedwill this work for audio files too?
For example my content Type has this field:
field_episode_audio
and my twig has:
src="{{ file_url(node.field_episode_audio.entity.fileuri) }}"
type="audio/mpeg">
The above example is meant to grab the .mp3 file placed into the Content Field: field_episode_audio when the user creates that content,
but it is currently not working (e.g. the audio does not load/play)
What do I need to do to get the URL of the .mp3 so I can render it into the src= ?
Comment #13
mindhunter75 commentedThank you thomas.frobieter for this code:
{{ file_url(content.field_node_image|field_target_entity.field_media_image.entity.uri.value) }}
You helped me a lot.
Comment #14
leisurman commentedThis adds a drupal image style and the image alt test
{{ file_url(content.field_featured_image|field_target_entity.field_media_image.entity.uri.value| image_style('featured_image')) }}" alt="{{ content.field_featured_image|field_target_entity.field_media_image.alt }}