Score:0

How do I get a media entity image's url/src in a block twig file?

so flag

I have a custom block type that has a media entity field that is an image. I want to extract that image url and set it as a div background image in the html.twig file, but I can't get it to work. The twig file is block--inline-block--hero-w-title.html.twig If I do

{{ content.field_hero_image[0] }}

it will display the rendered image with all the divs and stuff, but I can't figure out how to get the image url from content.field_hero_image. I have googled it in every way I can think of, and tried everything I have seen but can't figure out why nothing is working.

Do any of y'all know how to get the image url from media entity content.field_hero_image?

I have tried the listed and more so far:

content.field_hero_image|file_uri (twig_tweak. Failed due to sending an array when it was expecting object?) 
content.field_hero_image|file_url (twig_tweak: returned null) 
content.field_hero_image.entity.field_media_image.entity.uri.value (returned null) 
content.field_hero_image.entity.uri.value (returned null)
leymannx avatar
ne flag
Are you sure you've put the media to be displayed in the block's display settings? Which field formatter? Is the media entity's display settings configured correctly as well? Which field formatter?
Score:1
cn flag

The last lines look very promising for an image field nested in a media entity, you only have to start with the block content entity:

{{ elements.content['#block_content'].field_hero_image.entity.field_media_image.entity.uri.value }}

In a standard entity template, you would start with the name of the entity like {{ node. or {{ paragraph.. The block content entity doesn't have an entity template and in the block plugin template you need to look in the render element build array elements for the entity object.

Then use file_url() to transform the URI to a URL.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.