I have a block that has some fields that are rendered as part of a content type. In the block twig template file I am trying to render the image as a div background using:

<div style="background-image: url('{{ file_url(content.field_masthead_image['#items'].entity.uri.value) }}');">

I have also created a field for masthead_image in the content type. However, the image is not being rendered at all. I've checked permissions on the folder where the images are stored and they are 755. Any suggestions would be welcome.

Comments

kristin.e’s picture

I used a preprocess function to create a variable, 'masthead_image', and then passed it into the twig template as per suggestions from https://www.drupal.org/node/2408015 with:

<div style="background-image: url('{{ masthead_image }}');" >

I hope this helps someone.

leisurman’s picture

@kristin.e. Hi Can you please share the preprocess function you created for masthead_image?

jlbrewster’s picture

I have been spending a lot of time on this as well.  How did you write your custom function? 

rgracia’s picture