Change target to (target="_blank") for the link to an attachment
ElectricStudio.co.uk - February 6, 2007 - 16:36
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

Same question
I have the same request. Please tell me what is the solution for this problem.
Launch all http: on a new page
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...
Thanks trantt
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.
Thank you for your suggestion
Thank you for your suggestion, but can I ask you if you know where the link is being generated form in the first place?
Sorry, but I don't know, but
Sorry, but I don't know, but I also need it. So I'm looking for it too...
It is in the upload.module
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.
Certanly does.
I'm surprised this option isn't in the module. I can't remember the last time I created a link without _blank target...!
It works.
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
how about 6.2
I'm trying to add the _blank to one of my primary links in Drupal 6.2
I too
That code snippet above makes sense. However it doesn't seem to have an effect on my attachment hook.
Via Upload Attachement Override
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'));
}
}
Worked like a charm, as-is.
Worked like a charm, as-is. thanks!
works for other attributes too.
This ("Via Upload Attachment Override") works for other attributes too, such as rel="lightbox".
There is a module external
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
Does not work for internal links
Nice little module, but it is just for external links. Attachments are internal links, thus this module has no effect there.