0

I have a custom block, and a block--inline-block--hero-video.html.twig template file. The block has a field_video field.

If I put {{content.field_video[0]}} in my theme, the output is the following (as expected).

<span class="file file--mime-video-mp4 file--video">
  <a href="/sites/default/files/2023-09/birdcode-intro-1280.mp4" type="video/mp4">birdcode-intro-1280.mp4</a>
</span>
<span>(5.72 MB)</span>

I am looking to get only /sites/default/files/2023-09/birdcode-intro-1280.mp4.

According to Functions in Twig templates, the following should work.

{{ file_url(content.field_video.entity.uri.value) }}

Yet, that produces null, like the following markup.

{{ file_url(content.field_video.entity.uri.value) }}
{{ file_url(content.field_video[0].entity.uri.value) }}
{{ file_url(content.field_video[0].entity.uri )}}

It should be noted that you can define the field display as "URL" rather than "Generic file" in the admin interface, and if you do that, then {{content.field_video[0]}} will show /sites/default/files/2023-09/birdcode-intro-1280.mp4.

Nonetheless, I would like to extract the file URL in Twig in the case the field display is a generic file.

1
  • I've added a solution for block.html.twig to the linked topic.
    – 4uk4
    Sep 21 at 12:10

1 Answer 1

0

Try:

file_url(content.field_video['#items'].entity.uri.value)

Note this does not directly support translations.

Not the answer you're looking for? Browse other questions tagged or ask your own question.