2

I've installed the Custom Formatters module, as I could not figure out why my View setting was not working for "Rewrite output" -> "Target" -> "_blank"

I need to recreate either the "Generic File" format or the "Rendered File" -> "Link" format.

This is what I've tried but I suppose 'attributes' is not something I can add for the $variables array in theme() -

$output = '';
foreach ($variables['#items'] as $item) {
  $output .= theme('file_link', array(
    'file' => (object) $item,
    'attrs' => '_blank',
    ));
}
return $output;

That formatter code above was found on the CustomFormatters.com site for Generic Image

2

OK, found an answer that works for implementing the hook_file_link() function in the theme's template.php file:

Answer at https://drupal.org/node/301234#comment-4764468 - copied here:

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
  $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>';
}

Simply change THEMENAME to your theme name and place the code in the template file. I suppose I could extract this out into a custom formatter too.

0

Implemente the filefield hook(function) in theme/template.php file https://www.drupal.org/node/190566

<?php
function youtemplatename_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
  $new_window_mimetypes = array(
    'application/pdf',
    'text/plain'
  );
  if (in_array($file['filemime'], $new_window_mimetypes)) {
    $options['attributes']['target'] = '_blank';
  }

  return '<div class="filefield-file clear-block">'. $icon . l($link_text, $url, $options) .'</div>';
}
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.