Hi friends!

About using a file field and it's display settings:

Is there a module through which files uploaded via file field can be configured to open in a new window (by adding attribute "_blank" to a link to the file)? I think this is very often required by practice and such a tool is needed at the level of content type administration.

I found the solution described below looking on the Internet - by adding code in template.php - and this code works excellently, I added it directly and experimentally  in template.php of the core theme Bartik.


The code is:


function MYTHEME_file_link($variables) {

$file = $variables['file'];

$icon_directory = $variables['icon_directory'];

$url = file_create_url($file->uri);

$icon = theme('file_icon', array('file' => ($file instanceof File) ? $file : file_load($file->fid), 'icon_directory' => $icon_directory));

// Set options as per anchor format described at

// http://microformats.org/wiki/file-format-examples

$options = array(

'attributes' => array(

'type' => $file->filemime . '; length=' . $file->filesize,



// Use the description as the link text if available.

if (empty($file->description)) {

$link_text = $file->filename;

} else {

$link_text = $file->description;

$options['attributes']['title'] = check_plain($file->filename);


//open files of particular mime types in new window

$new_window_mimetypes = array('application/pdf', 'text/plain');

if (in_array($file->filemime, $new_window_mimetypes)) {

$options['attributes']['target'] = '_blank';


return '<span class="file">' . $icon . ' ' . l($link_text, $url, $options) . '</span>';


However, this solution is not very intuitive and easy for anyone, especially novice users of the system, and I do not know how secure and reliable it is in view of the future development of Backdrop CMS.


BWPanda's picture

I think that's the best way to do it.

I'm in the process of migrating a D7 site to Backdrop, and I had some method for doing this in D7 that wasn't ideal. So in my migration notes I have a note-to-self to:

Override theme_file_link() to open attached documents in a new window.

I haven't done it yet, but it'll be similar to yours and olafgrabienski's code. Don't think there's a better way...

BWPanda's picture

Just realised another way to do this would be to use Javascript to add target="_blank" to appropriate links.

This is how the External Links module does it.

I found more ways to upload downloadable PDF and text files so that they open in a new browser window or tab.

1. The file is uploaded to a directory on the server via a file browser or FTP, I also use IMCE for this purpose. You can also use a hidden file field to upload the file to the server, but display it with a link field.

The file is then presented on a page of the site, but not with a "file" field, but with a "link" field. This allows you to configure the target attribute, as well as many additional features.

If necessary, several files can be grouped using Field Group on link fields.


2. Instead of a field file, a long text field is used, and then, through simple HTML editing, the target attribute can be added to links in the text to download files.

An HTML table of uploaded files can also be created, which resembles the table of files uploaded via a file field.