Skip to content

Instantly share code, notes, and snippets.

@olafgrabienski
Created November 9, 2017 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olafgrabienski/960ee7e8f0b76128ce7bf12cb7d28880 to your computer and use it in GitHub Desktop.
Save olafgrabienski/960ee7e8f0b76128ce7bf12cb7d28880 to your computer and use it in GitHub Desktop.
Backdrop CMS: Open PDF and TXT files in new window
<?php
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>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment