Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By kristin.e on
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
Solved...
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.
@kristin.e. Hi Can you please
@kristin.e. Hi Can you please share the preprocess function you created for masthead_image?
Your hook code would be helpful
I have been spending a lot of time on this as well. How did you write your custom function?
I think she is referring to
I think she is referring to this solution: https://www.drupal.org/forum/support/theme-development/2015-01-15/prepro...