Thursday, August 17, 2017 09:34 am EDT

Theme Files to Open in a new window based on Mime Types

Bri's picture

There is a thread on drupal.org that describes how to theme drupal 6 filefields and Drupal 7 files to have them open in a new window based on the mime type. I am posting this here so that I can find it easier the next time I need to do so. I didn't write this code. I haven't tried the Drupal 6 code, but the thread appeared to have several people confirm that it worked. I myself can confirm that the code for drupal 7 works.

Add one of the following to your template.php file in your theme.

For Drupal 6 file fields

function THEMENAME_filefield_file($file) {
  // Views may call this function with a NULL value, return an empty string.
  if (empty($file['fid'])) {
    return '';
  }

  $path = $file['filepath'];
  $url = file_create_url($path);
  $icon = theme('filefield_icon', $file);

  // Set options as per anchor format described at
  $options = array(
    'attributes' => array(
      'type' => $file['filemime'] . '; length=' . $file['filesize'],
    ),
  );

  // Use the description as the link text if available.
  if (empty($file['data']['description'])) {
    $link_text = $file['filename'];
  }
  else {
    $link_text = $file['data']['description'];
    $options['attributes']['title'] = $file['filename'];
  }

  //open files of particular mime types in new window
  //add mime types to this array if you want more than just PDF files
  $new_window_mimetypes = array(
    'application/pdf',
    'text/plain'
  );
  if (in_array($file['filemime'], $new_window_mimetypes)) {
    $options['attributes']['target'] = '_blank';
  }

  return '
'. $icon . l($link_text, $url, $options) .'
'; }

For Drupal 7

function THEMENAME_file_link($variables) {
  $file = $variables['file'];
  $icon_directory = $variables['icon_directory'];

  $url = file_create_url($file->uri);
  $icon = theme('file_icon', array('file' => $file, '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
  //add mime types to this array if you want more than just PDF files
  $new_window_mimetypes = array('application/pdf','text/plain');
  if (in_array($file->filemime, $new_window_mimetypes)) {
    $options['attributes']['target'] = '_blank';
  }

  return '' . $icon . ' ' . l($link_text, $url, $options) . '';
}

Comments

Anonymous's picture

Thank you for essay writer

Thank you for essay writer information. I would try this setting. But still, I am not that much perfect in computers language. But I have understood the little but when applying If I found some difficulties I will definitely comment on your side for help. Till then thanks a lot for the information.

Anonymous's picture

Great !! Many thanks. This

Great !! Many thanks. This was very helpfull !!

Anonymous's picture

Works great! Thanks.

I love when I find the right solution within a minute of googling :) Thanks a lot!

Anonymous's picture

Thanks

Hey, thanks for putting this all in one place. Very helpful!