Custom Block Twig: File field how to get direct link to file? How can I get the direct URL to a file field in twig? I am able to do this for images, but with a file field, it comes up blank.

WORKS

{% set media_img_url = file_url(content.field_background_image.0['#item'].entity.uri.value) %}


DOESN't WORK

{% set media_file_url = file_url(content.field_cta_file.0['#item'].entity.uri.value) %}
#item is as far as I know only added by the image field formatter, so it's unlikely you'll find it in other field types.

Instead you can use the block content entity to get field values, for example:

{{ elements.content['#block_content'].field_file.0.entity.uri.value }}


This works independently of field formatters and as commented by @Kevin also when a field is hidden.