Last updated 3 October 2012. Created on 19 September 2005.
Edited by aardvark92, meitar, 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:

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 .= "
\n"; $output .= '

link) .'">'. check_plain($item->title) ."

\n"; $output .= "
$source $source_date
\n"; if ($item->description) { $output .= '
'. aggregator_filter_xss($item->description) ."
\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 .= '
'. t('Categories') .': '. implode(', ', $categories) ."
\n"; } $output .= "
\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 ?>

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:

$output .= '

link) .'">'. check_plain($item->title) ."

\n";

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

$output .= '

link) .'">'. check_plain($item->title) ."

\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.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

drupallogic’s picture

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

can someone update this please ???

equinox’s picture

*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!

GSpesatto’s picture

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

How I do?

innovative95’s picture

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.

trrroy’s picture

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?

jelo’s picture

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

lucio.ferrari’s picture

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.

kamydon’s picture

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

marcel.jtf’s picture

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

phunster’s picture

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.

mtrahan’s picture

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),

damd’s picture

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

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('%s',
                 $link,
                 $title);
}
ndoel’s picture

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

wael_2012’s picture

I installed feed aggregator module in my website and it has fantastic features but one disadvantage is that it redirect feed links to the source site that the feed belongs to.I want to redirect the feed item into the same site as if it is drupal article. how i can modify the module to do that thanks.