Last active
June 9, 2022 18:32
-
-
Save leahtard/328ad4ec8791643622ab00e13c9b8fd6 to your computer and use it in GitHub Desktop.
Get file url in node for Drupal 8 twig template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# 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 %} |
How I love Drupal templating!
Worth noting, this works with paragraphs too. Just swap out node
for paragraph
.
Brilliant. Also works with paragraphs like so: {{ paragraph.field_file.entity.uri.value }}
@brookeheaton thanks for share
this helped me a lot. thanks, dude! 👍
If you want to get the full url for internal URLs eg: private://
use
{{ file_url(node.field_file.entity.uri.value) }}
@frazras ... Thanks for your clarification also. Helped a lot.
I love it, thanks!!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Saved me alot. Thanks for share