2

I've created a teaser template like this node--type--teaser.html.twig. Within that template I want to conditionally create a link to a file that was uploaded via the field field_pdf.

How do you extract JUST the path to the pdf in the template.

I've tried {{ file_url(node.field_pdf.entity.url.value) }} but that just brings back an empty path.

If thats not possible, is there a way to get a file path from the fid in twig?

3
  • 1
    I think the field name is uri, not url.
    – 4uk4
    Commented Sep 18, 2018 at 21:39
  • 1
    Yo, thinking the same. See drupal.org/docs/8/theming/twig/… there it's {{ file_url(node.field_example_image.entity.uri.value) }}.
    – leymannx
    Commented Sep 18, 2018 at 21:49
  • Per the answer below, make sure the field type is set as File.
    – erier
    Commented Sep 19, 2018 at 14:44

3 Answers 3

5
  1. Manage teaser Display : Change Format of your file field to Generic File From admin/structure/types/manage/YouRContentType/display/teaser .
  2. In your twig node--type--teaser.html.twig just print {{ file_url(node.field_pdf.entity.uri.value) }} you will get the path to your file.
    Note : As mentioned in comment it's uri Not url.
5
  • Thanks for the response. What is this generic file setting? All i see is Rendered Entity, Entity ID, Label and Thumbnail as output options.
    – erier
    Commented Sep 19, 2018 at 13:38
  • is your field of type File ? if is the case From your Manage display page of your content type you will see a dropdown Column FORMAT some option RSS enclosure, Generic file, URL to file, Table of files .
    – berramou
    Commented Sep 19, 2018 at 14:14
  • Ah, thats probably what the issue is. I set the file type as Media. Updating this now.
    – erier
    Commented Sep 19, 2018 at 14:17
  • Thanks all for the help. The issue was as stated where I'd selected the Media field type instead of the File field type. With the File field type, the Generic File format is available in the teaser view mode.
    – erier
    Commented Sep 19, 2018 at 14:26
  • You are welcome, Can you just please add this as note to the question Field of type File just in case someone need it. Good luck.
    – berramou
    Commented Sep 19, 2018 at 14:33
1

In my case I had a paragraph type with a file field. I wanted to get a url of file-field inside paragraph's template. Not sure why but above solution of @berramou did not work for me.

So what I did is, in manage display I changed the file field's format to "URL to file". Then I rendered the file field, stripped its tags, trimmed and passed it to file_url like follows:

{% set url = file_url(content.field_pdf_file|render|striptags|trim) %}
0

In a field template file (for example, /themes/custom/y_subtheme/templates/field/field--node--field-brochure--course.html.twig), it will be {{ file_url(item.content['#file'].getFileUri()) }}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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