Re-theming the aggregator block

A nice person who shares the same name as a famous hair dude contacted me asking how to the drupal news aggregator block show teasers rather than simple titles. This is for the non-profit "The Epilepsy Support Centre". He pointed to this post where I reference themable function overrides in template.php. Go figure - this site must be googleable.

Theme overrides are really very simple for those brave enough to grep through the module code and do some experimenting. note: this is for drupal v 5.x but the process is the same for any version using a phptemplate based theme.

In this case a simple 'find all' search in aggregator.module for the term 'theme' returns a pretty short list of items, you will notice toward the bottom a few references to "@ingroup themeable". This is the good stuff.

The original code from the module begins line 1317

<?php/** * Format an individual feed item for display in the block. * * @ingroup themeable */function theme_aggregator_block_item($item, $feed = 0) {  global $user;  if ($user->uid && module_exists('blog') && user_access('edit own blog')) {    if ($image = theme('image', 'misc/blog.png', t('blog it'), t('blog it'))) {      $output .= '<div class="icon">'. l($image, 'node/add/blog', array('title' => t('Comment on this news item in your personal blog.'), 'class' => 'blog-it'), "iid=$item->iid", NULL, FALSE, TRUE) .'</div>';    }  }  // Display the external link to the item.  $output .= '<a href="'. check_url($item->link) .'">'. check_plain($item->title) ."</a>\n";  return $output;}?>

There's lots we can do away with here, like the 'blog it' links and the titles themselves since they are part of an aggregator teaser already. Since we know we want the block to mimic the teaser from the aggregator news page the next step is to find how that is generated. Scroll down to line 1355 and you'll see "Format an individual feed item for display on the aggregator page.", the function is "function theme_aggregator_page_item($item)" The part we want to steal is line 1377:

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

Put this all together in template.php inside your theme directory. Replace the part of the function name that reads theme_ with phptemplate_ and you are all set! Here's the final snippet:

<?php/** * Format an individual feed item for display in the block. * * @ingroup themeable */function phptemplate_aggregator_block_item($item, $feed = 0) {  // Display the feed teaser link to the item.  if ($item->description) {    $output .= '<div class="feed-item-body">'. aggregator_filter_xss($item->description) ."</div>\n";  }  return $output;}?>

The added bonus with this change is the block titles won't get ugly like they usually do with the check_plain() filter turning html into ascii characters. You can see the function for the aggregator_filter_xss() filter at line 1395:

<?php/** * Safely render HTML content, as allowed. */function aggregator_filter_xss($value) {  return filter_xss($value, preg_split('/\s+|<|>/', variable_get("aggregator_allowed_html_tags", '<a> <b> <br> <dd> <dl> <dt> <em> <i> <li> <ol> <p> <strong> <u> <ul>'), -1, PREG_SPLIT_NO_EMPTY));}?>

Happy drupal theming!