We have a need to set an element such as <div>
to have an image as a background. I am trying to figure out the way to get the image to display as a background but also with the image style that was used.
The image field (field_image) in question is used in multiple view modes with different image styles.
Example:
- Basic page view mode: default, field_image with format = image and image style = large
- Basic page view mode: promoted, field_image with format = image and image style = small
- Basic page view mode: small tile, field_image with format=image and image style = medium
How can I accomplish the code below? Would the proposed solution work as I am not sure it is safe to use ['#markup']
?
<div class="wrapper">
<div class="wrapper__image" style="background-image: url('{{ file_url(node.field_image.entity.uri.value) }}')"></div>
<div class="wrapper__content">
<!-- some text here -->
</div>
Currently on View mode:
- Field_image with Format = Image, Image Style = large
Current problem:
node.field_image.entity.uri.value
returns the original images, not the one where the image style is applied.
Proposed solution:
- set format = URL to image
- image style = large
- update the twig template to use
node.field_image.0['#markup]
style="background-image: url('{{ content.field_image }}');
in your template?<div>
of the field formatter. You usually can access the raw value of any field by adding the delta to it, e.g.{{ content.field_image.0 }}
sites/default/files/styles/large/image-url.png
. I want to get that URLcontent
render array, not the value from thenode
entity.