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.
What's the simplest way to get the file field to open in a new window when the link is using the description text? I can only seem to get it to target="_blank" when I display the full URL.
Comments
Comment #1
hillaryneaf CreditAttribution: hillaryneaf commentedI am using a view to show the field, so maybe there are more options available there with theming?
Comment #2
jbova CreditAttribution: jbova commentedYou may use the External Links module. You can configure the module's pattern matching field to match your files that should be opened in a new window. For example, to open all PDF documents in a new window, you can set the "Include patterns matching" field to the following:
.*\.pdf
Comment #3
OldAccount CreditAttribution: OldAccount commentedIf you're using Views, you can use the "Rewrite results" option for that field. I forget how it's worded in D6, but it's one of the options in the field configuration. Check the box that says "Rewrite the output of this field" and the one that says "Output this field as a link" and you'll get a bunch of link settings, including target.
However, I appreciate the tip given in comment #2 because I'm working on a D7 site and wanted to set the link target for a file field but I'm not using Views and I preferred to avoid using Display Suite because it's a PITA to get it working with a Rate widget. The External Links module and pattern-matching took 5 seconds to set up, much easier. Thanks!
Comment #4
Anonymous (not verified) CreditAttribution: Anonymous commentedI'm having the same issue in D7.
This may be ill-advised but if you like you can change the module/file/file.module file to add target="_blank" to all generic file links. Not wanting to install another module to open links in new windows I just changed the file directly. On my version I modified this, starting at line 734:
'type' => $file->filemime . '; length=' . $file->filesize,
),
to this
'type' => $file->filemime . '; length=' . $file->filesize,
'target' => '_blank',
),
Works for me so far.
Comment #5
quicksketchIt's ill-advised because you could easily accomplish your goal by copy/pasting the theme function you want to override into your theme's template.php. That way you can upgrade the module later and your theme contains the customizations. In this case, I think you would want to override theme_filefield_file(). See Overriding theme functions in the Drupal handbook.
Comment #6
jaesperanza CreditAttribution: jaesperanza commentedIn D6, there's a setting in your view > Fields (if you choose a fields-dependent Style) > Output this field as a link, then an option to open in new window.
Alternately, depending on your combined settings, you can set as Rewrite the output of this field > then enter custom code and fieldnames, example:
<a href=[your_file_pdf_upload] target=_blank>[your_field_title]</a>
You can put in other fancy tags like title and other scripts if accepted. Again, this is for D6 in case anyone has a need for this.
Comment #7
jaesperanza CreditAttribution: jaesperanza commentedIn D6, there's a setting in your view > Fields (if you choose a fields-dependent Style) > Output this field as a link, then an option to open in new window.
Alternately, depending on your combined settings, you can set as Rewrite the output of this field > then enter custom code and fieldnames, example:
<a href=[your_file_pdf_upload] target=_blank>[your_field_title]</a>
You can put in other fancy tags like title and other scripts if accepted. Again, this is for D6 in case anyone has a need for this.