Open aggregator links in new browser window

Last modified: June 25, 2008 - 10:08

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.

I assume this is a old info

drupallogic - March 9, 2009 - 08:34

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

equinox - April 4, 2009 - 21:41

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

GSpesatto - May 1, 2009 - 13:32

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

How I do?

use External Links module

innovative95 - May 7, 2009 - 22:52

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

tdohner - September 23, 2009 - 18:38

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?

 
 

Drupal is a registered trademark of Dries Buytaert.