- First commit to issue fork.
- 🇺🇦Ukraine seorusus Kyiv
In my case I 've got media url here
media_entity = content.field_hero_background.0['#media']
then
image_uri = media_entity.field_media_image.entity.fileuri
The completed code is{% if content.field_hero_background.0['#media'] %} {% set media_entity = content.field_hero_background.0['#media'] %} {% set image_uri = media_entity.field_media_image.entity.fileuri %} <div class="parallax__layer parallax__layer--back" style="background-image: url('{{ file_url(image_uri) }}');"> </div> {% endif %}
- 🇺🇸United States x_v
And if you want to apply an image style, you can simply add
|image_style('640x360')
For example:
{% set media_entity = content.field_media.0['#media'] %} {% set image_uri = media_entity.field_media_image.entity.fileuri|image_style('640x360') %}
- 🇿🇦South Africa kanthan
I jumped through hoops on this in D10.2.4. It seems that the actual image not in the media entity but is in a referenced file field, which contains references to file entities. To access the URL of the image, you'll need to retrieve the file entity associated with the field and then get the URL from that file entity.
Here's what finally worked for me. (My image is stored in a field called Feature Image with machine name field_feature_image):
{# extract a url from a media field #} {% if node.field_feature_image | length %} {# Get the first item from the media field #} {% set media_entity = node.field_feature_image.0. entity %} {# Check if the referenced entity exists and has a field_media_image #} {% if media_entity and media_entity.field_media_image | length %} {# Get the file entity associated with field_media_image #} {% set file_entity = media_entity.field_media_image.0. entity %} {# Get the URL of the image #} {% if file_entity %} {% set image_url = file_entity.uri.value %} <img src="{{ file_url(image_url) }}" alt="" class="img-fluid"> {% endif %} {% endif %} {% endif %}
- 🇮🇳India garglalit0
this will work in drupal 9 too get the image url
$image = $node->get('field_image_name')->getValue()[0]['target_id'];
$image = \Drupal::entityTypeManager()->getStorage('file')->load($image)->getFileUri();
$image_url = \Drupal::service('file_url_generator')->generateAbsoluteString($image);