Hi
Can some one please tell me what file I need to change to add a target for the file attachment links? Currently the attachment open up in the same window, but my client has requested to have them opening in a separate window.

In what file can I add the target="_blank" tag to the a href link for a uploaded, attached file?

Thank you for any help or advice. I'm using Drupal 5.1

Comments

sumsum’s picture

I have the same request. Please tell me what is the solution for this problem.

trantt’s picture

I'm not sure this is what you were looking for but basically, the below syntax will launch all http url address (url that started with http:) in a new browser:

1. Create a new template.php (or edit it if it was already there) --> under your themes folder (e.g themes/bluemarines)
2. Type in the followings:
<? php

function phptemplate_menu_item_link($item, $link_item) {
if (substr($link_item['path'], 0, 4) == 'http') {
return l($item['title'], $link_item['path'], !empty($item['description']) ? array('title' => $item['description'], 'target' => '_blank') : array('target' => '_blank'), isset($item['query']) ? $item['query'] : NULL);
}
else {
return l($item['title'], $link_item['path'], !empty($item['description']) ? array('title' => $item['description']) : array(), isset($item['query']) ? $item['query'] : NULL);
}
}

? >

Once again, I'm not sure this will fit with what you were trying to do...

sumsum’s picture

but it isn't good for me, and - I think - it not works.
So, I want to open a blank page when I click on an attachment. So the attachments "a" tag have to have a target:"_blank" option.
I'm new in drupal and in php programming, so sorry for my stupid questions. (And for my bad english too... :()
I hope this time it is clearlier.

ElectricStudio.co.uk’s picture

Thank you for your suggestion, but can I ask you if you know where the link is being generated form in the first place?

sumsum’s picture

Sorry, but I don't know, but I also need it. So I'm looking for it too...

bottdev’s picture

I am using 4.7.6 but I am guessing there is something similar in 5.1.

In upload.module, you can add attributes to the link in the attachments table. The core code is:

function theme_upload_attachments($files) {
  $header = array(t('Attachment'), t('Size'));
  $rows = array();
  foreach ($files as $file) {
    if ($file->list) {
      $href = $file->fid ? file_create_url($file->filepath) : url(file_create_filename($file->filename, file_create_path()));
      $text = $file->description ? $file->description : $file->filename;
      $rows[] = array(l($text, $href), format_size($file->filesize));
    }
  }
  if (count($rows)) {
    return theme('table', $header, $rows, array('id' => 'attachments'));
  }
}

and you will have to change it to

function theme_upload_attachments($files) {
  $header = array(t('Attachment'), t('Size'));
  $rows = array();
  foreach ($files as $file) {
    if ($file->list) {
      $href = $file->fid ? file_create_url($file->filepath) : url(file_create_filename($file->filename, file_create_path()));
      $text = $file->description ? $file->description : $file->filename;

      //Added Attributes below to allow the uploaded file to open in a separate window
      $attributes = array();
      $attributes['target'] = "_blank";
      $rows[] = array(l($text, $href, $attributes), format_size($file->filesize));
    }
  }
  if (count($rows)) {
    return theme('table', $header, $rows, array('id' => 'attachments'));
  }
}

I hope this helps in the 5.1 world.

vannen’s picture

I'm surprised this option isn't in the module. I can't remember the last time I created a link without _blank target...!

niek_kloots’s picture

I'm using the solution from http://drupal.org/node/190035 for RSS to open in a new window.

All of my external links open in a new page!
And it works with Drupal versions 5.1 , 5.3 and 5.5

jcbrew’s picture

I'm trying to add the _blank to one of my primary links in Drupal 6.2

mikeque’s picture

That code snippet above makes sense. However it doesn't seem to have an effect on my attachment hook.

wadley0’s picture

This worked for me:
Add the following code into the template.php file of your theme. This is based on the code from bottdev in the post above, but updated for drupal 6. The key in the update is that passing attributes to the link function has been modified. See api for function l().

/**
 *  Override for attachments to be target="_blank"
 */
function phptemplate_upload_attachments($files) {
  $header = array(t('Attachment'), t('Size'));
  $rows = array();
  foreach ($files as $file) {
    $file = (object)$file;
    if ($file->list && empty($file->remove)) {
      $href = file_create_url($file->filepath);
      $text = $file->description ? $file->description : $file->filename;
			
      $rows[] = array(l($text, $href, array('attributes' => array('target' => '_blank'))), format_size($file->filesize));
    }
  }
  if (count($rows)) {
    return theme('table', $header, $rows, array('id' => 'attachments'));
  }
}

mikeque’s picture

Worked like a charm, as-is. thanks!

droddis’s picture

This is great and works perfectly. Do you have any suggestions on how I could apply it to CCK file fields that I've uploaded?

Because those files weren't uploaded with the upload module the code as written isn't applied. I'm thinking there must be an easy way to modify the code to get it to apply to those fields as well.

All help appreciated!

Cheers,

wadley0’s picture

This ("Via Upload Attachment Override") works for other attributes too, such as rel="lightbox".

Road Runner’s picture

There is a module external links that solves this problem. In the site config part there is option to open all links in new window. Works in 5 and 6

mikeque’s picture

Nice little module, but it is just for external links. Attachments are internal links, thus this module has no effect there.