Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By pwetter on
As the subject says: Simply, how in Drupal 8 do I get a file linked on a page to open in a new window/tab (target="_blank"). I don't see anything in the interface and can't find a module that seems to support this. Back with my Drupal 6 sites, i could hack a file and add the attribute. Can I modify a theme somehow to do this? Can someone give me a quick tutorial?
Thanks much!
Comments
Workaround Solution
So, found something that get's the job done, but probably isn't the proper solution.
Modify: /core/modules/file/file.module
Find the template_preprocess_file_link function.
Below:
Add:
Don't Hack Core!
This violates rule #1 (at least my rule #1) when using Drupal: "Don't hack core."
Go with the suggestion below, as I am going to, having come across this page looking for the same thing. FWIW, I'm also going to try adding download as an attribute.
Michael
As I said, “probably not a
As I said, “probably not a proper solution”, like duct tape. Once it was posted, I did go with the hook addition to my theme to fix the issue
I was stuck with a hackish solution as well
Unfortunately, I had to create a custom template for the containing entity (a paragraph in my case) of the media file and hardcode the
<a>
tag withdownload
as an attribute. Very unsatisfying.Michael
Use a Preprocess Hook
If you prefer not to update core, use a hook_preprocess_file_link function in your theme file to add the target attribute on the generated link. Here's an example:
By the time this hook runs, the file link HTML has already been generated. So, we have to parse the HTML into a DOMDocument object, set the target attribute and then write the modified HTML back out into the generated link.
Thank you! This works
Thank you! This works perfectly! Exactly what I was looking for.
Does it would work for link
Does it would work for link field data ? Because actually it does nothing on my website.
Dev Server Shared Hosting
8 websites powered by drupal 6,8 & 9 - Hosted by Always Data
No. In my experience, it
No. In my experience, it only opens links to files in a new window. Not, general links. My issue was dealing with file field links that I wanted to open in new tab.
for linkfields, this module
for linkfields, this module give option to choose attribute: https://www.drupal.org/project/link_attributes/issues/3055369
but not to set it by default, for the moment.
Dev Server Shared Hosting
8 websites powered by drupal 6,8 & 9 - Hosted by Always Data
Hi. I try your code but file
Hi. I try your code but file field aren't opened in blank and attributes isn't change.
I put your code into my .theme file in replacement of my buggy code (that work until D8.8.1).
Dev Server Shared Hosting
8 websites powered by drupal 6,8 & 9 - Hosted by Always Data
Preprocess Hook
For media files links that use instanceof \Drupal\Core\Url, alternative hook process is:
This will add the target under object url attributes:
This code solved my issue,
This code solved my issue, Thank you adominique
Maybe a new Field Formatter?
I made a Field Formatter on a recent project for creating more configurable file download links. You get to choose whether or not to open the file in a new tab, whether the browser should add the download attribute to the link, and the link text. You could put this in a custom module at your_module/src/Plugin/Field/FieldFormatter. Depending on what link text you want to display, it might not work for you, but I thought it would be worth throwing out there.
Please create a project with
Please create a project with a ready-made module.
Here's the field formatter module
https://www.drupal.org/project/file_download_link
Let me know if there are any problems.