I'm on d7. I have two content types on my site, "link" and "Article". The link content type contains a link field from the link module that users enter a link to another site in to. I have the "link" content type referenced (thanks to the entity reference module) in my "article" content type and I would like for the link, when clicked, to open the referring page (offsite) in a new tab/window. How can I do this?? Any suggestions? Thanks.
4 Answers
You can configure a link field to open in a new window.
About halfway down the edit page for your link field, you'll see 'Link Target,' and that's where you set it.
Have you tried to add a simple jQuery script that adds "target='_blank'
to the link field?
For example, if your link field ID is "field_link_und_0" (search it in the HTML source), in the node-XYZ.tpl.php template file you can add the following code.
jQuery(document).ready({
jQuery('#field_link_und_0').attr("target","_blank");
});
This is the real stuff:
just make this function in your template.php. It comes from file.module:
function **yourtheme**_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,
'target' => '_blank',
),
);
// 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);
}
return '<span class="file">' . $icon . ' ' . l($link_text, $url, $options) . '</span>';
}
I think there were two questions in one.
- How to open the offsite URL in the referred node (Link) directly from the referring node (Article). For that, use the Link Replace Filter module.
- How to open an offsite URL in a new browser tab — the easiest is to use for that is the External Link module.