Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get file url in node for Drupal 8 twig template
{# Get URL of single file field #}
{{ node.field_file.entity.uri.value }}
{# Get URL of multi file field #}
{{ node.field_file['#items'].entity.uri.value }}
{# Trun into link #}
{{ file_url(node.field_file.entity.uri.value) }}
{# Check if there is at least one value #}
{% if node.field_file[0] %}
// Do something...
{% endif %}
@bryankimani
Copy link

bryankimani commented Nov 3, 2017

Saved me alot. Thanks for share

@danjdewhurst
Copy link

danjdewhurst commented Nov 10, 2017

How I love Drupal templating!

Worth noting, this works with paragraphs too. Just swap out node for paragraph.

@brookeheaton
Copy link

brookeheaton commented Feb 8, 2018

Brilliant. Also works with paragraphs like so: {{ paragraph.field_file.entity.uri.value }}

@bryankimani
Copy link

bryankimani commented Jul 25, 2018

@brookeheaton thanks for share

@novia713
Copy link

novia713 commented Feb 8, 2019

this helped me a lot. thanks, dude! 👍

@frazras
Copy link

frazras commented Apr 11, 2019

If you want to get the full url for internal URLs eg: private:// use
{{ file_url(node.field_file.entity.uri.value) }}

@wakudrle
Copy link

wakudrle commented Apr 18, 2019

@frazras ... Thanks for your clarification also. Helped a lot.

@fr0standsn0w
Copy link

fr0standsn0w commented Oct 31, 2021

I love it, thanks!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment