I think would be very useful to have the ability to set in "Display Field Settings" and Views an option to have the file open a new window. This can be easily themed, but for non-coders this would be a simple, handy feature. Thanks! Let me know if I can help in any way.

Files: 

Comments

To build on this idea, what about having some options like:

Display Filename as Link to File
Display Node Title as Link to File
Display Filename as Link to File in New Window
Display Node Title as Link to File in New Window
etc.,

For Views, I would love to have an icon-only field display option for table views. Right now, there is only the "Default" option which shows title (as link) and icon.

Status:Active» Closed (won't fix)

Write your own formatter... This isn't going into filefield.

Version:5.x-2.x-dev» 6.x-3.2
Status:Closed (won't fix)» Active

Unless I can't see it anywhere this feature didn't get implemented in D6 version. Such an option would be very useful.

We discussed this once in the D6 queue and I did implement the link opening in a new window, but only on the node edit form, since leaving the page would cause your new uploads to be lost. However this still won't be added to the front-facing file display. I'd suggest putting some jQuery in your theme such as this:

$('.filefield-file a').attr('target', '_blank');

Or you can use any number of popup scripts out there to make your files open in a window that is a certain size.

Status:Active» Closed (won't fix)

edit function theme_filefield_file in filefield_formatter.inc
best way is to add to your template.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
  // http://microformats.org/wiki/file-format-examples
  // TODO: Possibly move to until I move to the more complex format described
  // at http://darrelopry.com/story/microformats-and-media-rfc-if-you-js-or-css
  $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 allfiles in new window
$options['attributes']['target'] = '_blank';
  return '<div class="filefield-file clear-block">'. $icon . l($link_text, $url, $options) .'</div>';
}

Ok!, it is everything correct.

Thank you very much, I brought while attempting to find the solution.

Sincerely,

Xavier Garcia

Try the following with slight modifications to open in a new window based on the MIME type. The idea was borrowed from an idea jpetso said here http://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>';
}
?>

I like the suggested solution in #1. That would be very cool.

#7 works great for me. Thanks for posting.

#7 resolve my problem too, great job!

#7 solve my interrogation...great thx

#7 and #9 solve my problen ...great
Thanks ali_b and dppeak

#7 and #9 solutions are great! Thanks!

I wound up going with #9, which works fine but shouldn't be required just to get this kind of functionality, IMNSHO. I had to update code in SVN, migrate the changes onto a server, test it, etc before deploying when it would have been great to have either told the client (or done it myself) to just click a checkbox that said "Open in new window" on the field settings page and hit "Save Settings."

The Link CCK field offers it as an option on its field (along with quite a few other nice options like link text on/off/optional/static which would have been handy on so many occassions for this field, but I digress), not quite sure why there is so much resistance to adding this feature when so many people have asked for it?

It'd make the module a lot more user-friendly for those people setting up Drupal sites that are not ripping into the template files, or people administering sites that have no clue how (and whose job it isn't) to hack on a template file...

I agree with #16, couldn't said it better myself.

User friendliness is key.

Thank you for this, exactly what I needed. #9 worked great.

#9 worked great when i changed in the filefield_formatter.inc
but when i copy this function to mu template.php it doesnt work!!!!
what is my problem?????

Status:Closed (won't fix)» Needs review
Issue tags:+Field Display
StatusFileSize
new55.75 KB

Hi,

Code base solution (in theme template.php file) is not the best way since:

  • you must to apply this in every website you create
  • in case of a sub-theme, you have to copy base theme template.php and merge it in every theme update
  • any filefield module theme related update should be shifted into you code

I think a better (and complete) approach should be a new field display related settings, something like proposed in attached screenshot.

Status:Needs review» Closed (won't fix)

Sorry, this isn't going to happen. A theme-based solution is the best one in my opinion. FileField has been moved into core for Drupal 8 and won't be changing any more functionality. At least that means that you're safe to override the code in your theme, since it shouldn't change for the lifetime of Drupal 6 or 7.

Ok, I'll made this change for my personal drupal base installation. I don't like to do the same work once and once again for every website I made. If somebody wants the patch, just ask me. Thx.

Adan, can you please send me the patch or indicate where I can download the patch.

Thanks!

How can I use "open a File in new window" in drupal 7?

#24 @rmwolinski
Hope this helps: http://drupal.org/project/extlink

I tried this module but this is for external links. Maybe I can't configure it.

The solutions given above do not work for Drupal 7 due to changes in the filefield module in core. Use the following code (placed in your template.php file) to accomplish the same as #9 in 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
  $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>';
}

#27 is working great :)

#27 works :) for D7 thanks
do you think is it possible to do the same but with the W3C javascript standards ?

onclick="window.open(this.href); return false;"

#27 did it for me too. Thanks!

I inserted code from #27 in my template.php and changed the THEMENAME. My PDFs are still opening in same window. Am I missing something?

I'm in the same boat. Not having any luck getting them to open in a new window. It would appear the function isn't being called to create the link. Any hints?

#27 worked for me as well.... 1 minute and I found it G0ogle.... saved me much time to write from scratch... thanks!

*having trouble with not opening in new links after you insert the code in your theme template? Must flush cache any time you update theme template unless using a mechanism which is clearing it for you (90% of the time you will need to manually flush, so just do that anyway)

Thank you - #27 worked for me in Drupal 7 (after clearing the site cache.)

Both #7 and #9 worked for me.

Thanks for sharing this all!!

thankful, #7 #9 #27 and #34

Though many of the above methods are valid to do this, you could also use JQuery (attribute), I tried this and it works. Obviously your specific code might change a bit depending on what you are targeting. This may not be as comprehensive for some of the methods above but in some cases may be fine. In this example, I had a filefield called "App Landing". This would work for Drupal 6 but could be adapted for 7 as well.

//open docs in new tab or window (pdfs)
Drupal.behaviors.mythemetarget = function (context) {
  if (jQuery().attr) {
$(document).ready(function(){
  $('.field-field-app-landing-files a', context).attr('target', '_blank');
});
}}

Note change "mytheme" to your theme name and refer to # 39 below for instructions how to add this.

#highrockmedia -

Where do you put this, JQuery (attribute)?

@colorado -- you could do it a few ways.

  1. You could create a "scripts.js" file or whatever you want to call it (and add the customized code snippet as above according to your specific HTML) in your theme folder. I like having a dedicated scripts folder such as "js" or "scripts". Then in your theme's .info file you could call the script like: scripts[] = js/script.js -- make sure to clear your site cache after creating and adding code / files.
  2. use the Drupal add JS method. There are various ways you can do this but commonly done in template.php. This is more for one off things where you might need to just add the script to a single page. If it's something more sitewide, then stick to #1 above. http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_ad...

OK then. Thank you very much!

#27 worked for me. thanks a bunch!

if it doesn't work for you, flush the cache. i just replaced the THEMENAME with my own theme name, added some array elements on $new_window_mimetypes, flushed the cache and tadaaaaaah! ^^

#27 worked just as expected. From an end-user perspective it's a shame the options won't even make it to D8.

#27 worked for me - thanks to all of you!

#27 worked the way I wanted. Perfect! Thank you

#27 solution works fine. Thanks!

FWIW, #27 works here too (in D7).

Thanks a lot. Tried #27 works perfect on D7.

Thanks a lot. Tried #27 works perfect on D7.

Me too. Thank you.

Thanks. #27 works in Drupal 7.
Is there anyway to adapt that so that html files that are uploaded can open in a new window?

I have done as #27 told me. Copied the code into my template.php file, replaced the "THEMENAME" with my actual theme name. After saving, I cleared the cache on my website. But for some reason, this still isn't working.

I have very little knowledge of PHP, so figuring out the problem would be very hard for me. Is there anyone who might know how to solve this?

#27 still works great.

#27 Thanks...It works great.

#27 worked perfectly for me in drupal 7... thank you!

#27 worked for me in drupal 7. thank you!!!

27# worked for me. Had to replace the YOURTHEMENAME in the code with mine lol.

Silly I always forget that!

This is a nice feature for ease of use. A lot of requests on websites for this type thing.

Scott
www.coverclubmedia.com

Issue summary:View changes

#27 worked a treat for me... thank you, @jasondecamp - almost three years on!