1

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?

  • 1
    I think the field name is uri, not url. – 4k4 Sep 18 '18 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 Sep 18 '18 at 21:49
  • Per the answer below, make sure the field type is set as File. – erier Sep 19 '18 at 14:44
4
  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.
| improve this answer | |
  • 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 Sep 19 '18 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 Sep 19 '18 at 14:14
  • Ah, thats probably what the issue is. I set the file type as Media. Updating this now. – erier Sep 19 '18 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 Sep 19 '18 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 Sep 19 '18 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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