Open aggregator links in new browser window

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.

 
 

Drupal is a registered trademark of Dries Buytaert.