Community Documentation

Open aggregator links in new browser window

Last updated June 25, 2008. Created by meitar on September 19, 2005.
Edited by add1sun, Amazon. Log in to edit this page.

Many people would like to keep users on their site and in some situations you may find opening aggregator links in a new window accomplishes this. (Be warned, however, that this tactic is sometimes considered underhanded and is generally not accessible.) In order to have users open links in a new browser window instead of leaving your site you can either look at the External Link module or override the aggregator code in your template.php:

Find the function to override

First open aggregator.module in your favorite text editor. Then locate this function in the aggregator.module file:

<?php
function theme_aggregator_page_item($item) {

 
$source = '';
  if (
$item->ftitle && $item->fid) {
   
$source = l($item->ftitle, "aggregator/sources/$item->fid", array('class' => 'feed-item-source')) . ' -';
  }

  if (
date('Ymd', $item->timestamp) == date('Ymd')) {
   
$source_date = t('%ago ago', array('%ago' => format_interval(time() - $item->timestamp)));
  }
  else {
   
$source_date = format_date($item->timestamp, 'custom', variable_get('date_format_medium', 'D, m/d/Y - H:i'));
  }

 
$output .= "<div class=\"feed-item\">\n";
 
$output .= '<h3 class="feed-item-title"><a target=\"_blank\" href="'. check_url($item->link) .'">'. check_plain($item->title) ."</a></h3>\n";
 
$output .= "<div class=\"feed-item-meta\">$source <span class=\"feed-item-date\">$source_date</span></div>\n";

  if (
$item->description) {
   
$output .= '<div class="feed-item-body">'. aggregator_filter_xss($item->description) ."</div>\n";
  }

 
$result = db_query('SELECT c.title, c.cid FROM {aggregator_category_item} ci LEFT JOIN {aggregator_category} c ON ci.cid = c.cid WHERE ci.iid = %d ORDER BY c.title', $item->iid);
 
$categories = array();
  while (
$category = db_fetch_object($result)) {
   
$categories[] = l($category->title, 'aggregator/categories/'. $category->cid);
  }
  if (
$categories) {
   
$output .= '<div class="feed-item-categories">'. t('Categories') .': '. implode(', ', $categories) ."</div>\n";
  }

 
$output .= "</div>\n";

  return
$output;
}
?>

Use template.php to override

Copy the aggregator code above.

Next open the themes/the_active_theme/template.php file. In this case I am using the default Garland theme so I open themes/garland/template.php and paste the code we just copied at the end of the file right before the closing php tag ?&gt;

Next change the signature of the function we just pasted from function theme_aggregator_page_item($item) to function garland_aggregator_page_item($item).

Now we can hunt down the code that we can change to make links open in a new window. In this case it looks like this:

<?php
$output
.= '<h3 class="feed-item-title"><a href="'. check_url($item->link) .'">'. check_plain($item->title) ."</a></h3>\n";
?>

we just are going to add target="_blank" we have to escape the quotes so it will look like this

<?php
$output
.= '<h3 class="feed-item-title"><a target=\"_blank\" href="'. check_url($item->link) .'">'. check_plain($item->title) ."</a></h3>\n";
?>

Aggregator links in new window with jQuery

Rather than override the aggregator functions and change the resulting HTML, jQuery can also be used to achieve the same effect, but in a far more gracefully degrading way instead.

To do this, add the following JavaScript code to a template file.

/* find all aggregator links that have 'http' within the href and add a target */
$(document).ready(function(){
    if($("#aggregator a[@href*=http]")[0]) {
        $("#aggregator a[@href*=http]").each(function(i) {this.target = "aggwin";});
    }
});

If this does not work in some instances, particularly for anonymous users, the cause may be because nothing else in the page is calling the core Drupal JavaScript. To fix this, you may simply need to add a hard-coded call to misc/jquery.js in page.tpl.php to ensure the appropriate JavaScript library gets loaded.

Comments

I assume this is a old info

I assume this is a old info cause I don't see mentioned function in aggregator.module file.

can someone update this please ???

Here is how I launched aggregator links in a new tab

*This "tactic" is not remotely underhanded these days, it is how feeds should operate. Tabbed browsing makes this appropriate, so I think the article above is "quite" dated.

I am using Drupal 6.10
In drupal_root/modules/aggregator/ you are going to modify two files and save them into your themes folder. Do not save this file back into your /modules folder.. instead save it into your themes folder where your template.php resides, otherwise it will just be overwritten next time you do a routine upgrade of Drupal.

First open aggregator-item.tpl.php

Find this line:

<a href="<?php print $feed_url; ?>" >

and replace it with

<a href="<?php print $feed_url; ?>" target="myfeed">

where "myfeed" is any window target identifier you want to use so that each feed will open into that same new tab. Use "_blank" if you want every link to open in its own new tab, but that's usually not the best way for a feed to work, in my opinion.

In this same file make this change:

<a href="<?php print $source_url; ?>" class="feed-item-source">

and replace it with

<a href="<?php print $source_url; ?>" class="feed-item-source" target="myfeed">

Write the modified file into your themes folder:for example /themes/garland or in my case /themes/colorise

You must do this same step to one more file - the file that controls the summaries listing on your "Source" page

Modify aggregator-summary-item.tpl.php

Find this line:
<a href="<?php print $feed_url; ?>" >

and replace it with

<a href="<?php print $feed_url; ?>" target="myfeed">

Save it also to your themes folder and not to the /modules/aggregator folder

Hope this works for you!

How launch bock aggregator links in a new tab?

I want to open up a block aggregator links in new tab.

How I do?

use External Links module

Hi,

use the external links module. it will open all external links in
new window.

I tried the method given by the user above - modifying the files -
for some reason it didn't work.

how to get the new templates working

Hi equinox, I followed your instructions but it doesn't seem that the new templates (in the current theme directory) are being used. Is there another change that needs to be made to have the new .tpl.php files applied to Aggregator?

Feed items in block

Hi equinox,

Thanks for this information. Works fine by adjusting the template files. If you want the source URL to display in the new window, you need to adjust the file aggregator-feed-source.tpl.php as well (somehow the RSS icon though still loads in the same window).

However, I could not yet get the block display to open the items in a separate window. The aggregator modules creates a block for each feed named "title latest items". How do I change it so that the items open in a new window?

Thank you, Jens

Hi all, I couldn't find the

Hi all,
I couldn't find the aforementioned function neither, so I overrode this instead:

(your theme name here)_aggregator_block_item($item, $feed = 0) {
  global $user;

  $output = '';
  if ($user->uid && module_exists('blog') && user_access('create blog entries')) {
    if ($image = theme('image', 'misc/blog.png', t('blog it'), t('blog it'))) {
      $output .= '<div class="icon">'. l($image, 'node/add/blog', array('attributes' => array('title' => t('Comment on this news item in your personal blog.'), 'class' => 'blog-it'), 'query' => "iid=$item->iid", 'html' => TRUE)) .'</div>';
    }
  }

  // This is the line I edited adding target="_blank"
  $output .= '<a target=\"_blank\" href="'. check_url($item->link) .'">'. check_plain($item->title) ."</a>\n";

  return $output;
}

and I put it in my themes/themename/template.php. Flush cache, and there you go. Or at least it worked for me.
Hope this helps.
L.F.

Works!

Thank you lucio.ferrari !!! Only your solution worked! Thanks!
I tried the previous ones but they didn't work. Yours worked!

Works !

Thanks lucio.ferrari. Your solution works perfect.
I was also not able to use the other solutions mentioned.

How-to with Aggregator 6.19

I've got Aggregator 6.19 and like another commenter could not find the module code pointed to in the original post. However, I did find this:

function theme_aggregator_block_item($item, $feed = 0) {
  global $user;

  $output = '';
  if ($user->uid && module_exists('blog') && user_access('create blog entries')) {
    if ($image = theme('image', 'misc/blog.png', t('blog it'), t('blog it'))) {
      $output .= '<div class="icon">'. l($image, 'node/add/blog', array('attributes' => array('title' => t('Comment on this news item in your personal blog.'), 'class' => 'blog-it'), 'query' => "iid=$item->iid", 'html' => TRUE)) .'</div>';
    }
  }

  // Display the external link to the item.
  $output .= '<a href="'. check_url($item->link) .'">'. check_plain($item->title) ."</a>\n";

  return $output;
}

It seemed like a good candidate to accomplish the goal so I copied it into my template file in my custom theme directory, and added the target blank like so:

function my_custom_theme_name_aggregator_block_item($item, $feed = 0) {
  global $user;

  $output = '';
  if ($user->uid && module_exists('blog') && user_access('create blog entries')) {
    if ($image = theme('image', 'misc/blog.png', t('blog it'), t('blog it'))) {
      $output .= '<div class="icon">'. l($image, 'node/add/blog', array('attributes' => array('title' => t('Comment on this news item in your personal blog.'), 'class' => 'blog-it'), 'query' => "iid=$item->iid", 'html' => TRUE)) .'</div>';
    }
  }

  // Display the external link to the item.
  $output .= '<a target=\"_blank\"a href="'. check_url($item->link) .'">'. check_plain($item->title) ."</a>\n";

  return $output;
}

It worked for me.

Thanks to the original poster for reminding me, by my reading the post, that I meant to do that.

External New Tab module

None of these solutions seemed to work for me (I use a block for a category with multiple feeds, not a block per feed — just saying, but I don't really think that's the problem), so I tried the very simple External New Tab module that you can find there: http://drupal.org/project/external

You just install, activate, and it works right away (without the little "external links" icons from the module of the same name — that I didn't want).

Good luck (and thanks to everyone sharing ideas here),

What worked for me was this

This is what I put in my theme's template.php file and it worked instantly:

<?php
function MY_THEME_aggregator_block_item($variables) {
 
$link = check_url($variables['item']->link);
 
$title = check_plain($variables['item']->title);

 
// I prefer sprintf to string concatenation.
 
return sprintf('<a target="feed-news" href="%s">%s</a>',
                
$link,
                
$title);
}
?>

Deniz Dogan

Thanks damd, i'm use drupal

Thanks damd, i'm use drupal 7.10 and "Beale Street" theme, this work for me.

About this page

Drupal version
Drupal 5.x, Drupal 6.x
Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.
nobody click here