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

thomas.frobieter created an issue. See original summary.

thomas.frobieter’s picture

The 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)

leisurman’s picture

@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.

<div style="background-image: url('{{ file_url(content.field_global_image|field_target_entity.image.entity.uri.value) }}')">
  <div>
    <h1>Namet Sem Dolor Purus Elit Cras Elit Dolor Tellus Bibendum Aenean Ridiculus Mattis</h1>
  </div>
</div>
Ralf Eisler’s picture

Works 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')) }}

leon kessler’s picture

FWIW, 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.

rupl’s picture

Is 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:

{{ file_url(product.field_images[0]|field_target_entity.field_media_image.entity.uri.value) }}

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:

{{ file_url(product.field_images|field_target_entity[INDEX].field_media_image.entity.uri.value|image_style('thumbnail')) }}
drewid’s picture

Thank 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.

adrian_s_m’s picture

This 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:

{% set image_url1 = file_url(content.field_block_image[0]['#media'].field_media_image.entity.uri.value) %}
{% set image_url2 = file_url(content.field_block_image[1]['#media'].field_media_image.entity.uri.value) %}
{% set image_url3 = file_url(content.field_block_image[2]['#media'].field_media_image.entity.uri.value) %}	
{% set image_url4 = file_url(content.field_block_image[3]['#media'].field_media_image.entity.uri.value) %}

and then

<a href="{{ content.field_link_to_content[0]['#url'] }}" class="image-box" style="background-image: url('{{ image_url1 }}')">

sutharsan’s picture

Status: Active » Fixed

Closing the issue as it seems to be resolved.

thomas.frobieter’s picture

Worth 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) }}

Status: Fixed » Closed (fixed)

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

auxiliaryjoel’s picture

will 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= ?

mindhunter75’s picture

Thank 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.

leisurman’s picture

This 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 }}