I'm trying to link to a file in a view. I have a field in the view that outputs the link if I use it like this:

<div class="product-cta-view-pdf">
  {{ fields.field_notice.content }}

The output: /sites/default/files/products/notice/test.pdf

What I want to do is use that to create a link to the pdf to view (and another one to download). How can I use this in the href of a link?


You can use the drupal build in file_url twig function for that:

{{ file_url( fields.field_notice.content) }}

You can find more information about the Drupal build in twig functions here:


  • this opens the pdf viewer but doesn't show any content.. it just keeps loading – FonsVC Mar 3 at 12:19
  • As it opens a pdf viewer, your browser seems to know it is about a pdf. It sounds that the link is correct. What is the html output between the <div class="product-cta-view-pdf"> and </div>? If you follow that link manually does it work? If not you probably have a corrupted file. – Rick B Mar 3 at 12:27
  • it outputs: <a href="#pdf" class="product-brochure-link">Voir notice</a> – FonsVC Mar 3 at 12:46
  • Ok so it seems that the output was not just the link in plain text in html output. I have to guess a little now, but try the following: {{ file_url( fields.field_notice.uri.value) }} If that doesnt work, try inspecting the variable for the correct link: drupal.org/docs/8/theming/twig/… – Rick B Mar 3 at 13:08
  • When I try it with .uri.value, I get "/" as href. And if I try to dump anything, it crashes – FonsVC Mar 3 at 14:05

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.