Last updated 23 November 2009. Created on 23 April 2008.
Edited by lhtown. Log in to edit this page.

This snippet works with the core aggregator module in Drupal 5. For Drupal 6 and later, use the Views module instead of this code.

Place the following code in a block to display a list of all aggregator items. The number of aggregator items that appear is controlled by how long aggregator items are kept (in Drupal, not this snippet) since this snippet is set to display all available items.

This snippet will display all available aggregator item headlines along with their publication date in a single column with the newest items displaying at the top of the list.

This snippet was written to avoid having teasers or body content display because of copyright concerns as well as other reasons. If you don't want teasers to appear publicly anywhere on your site, you can set the user permissions so only the administrator has rights to access aggregator content. Alternatively, (this is a dirty fix), you can delete (at your own risk) the column from your mysql database that holds the teasers in the aggregator_item table. Another way to do this is to use a url redirect to redirect the aggregator pages to the pages you want to be seen.

This snippet is appropriate for use in a block or a page. It was tested with Drupal 5. Your comments are appreciated.

<?php
// Last update: April 23, 2008
$result = db_query("SELECT a.title, a.link, a.timestamp
    FROM {aggregator_item} a
    ORDER BY a.timestamp DESC");
$output = '<div>';
while ($feed = db_fetch_object($result)) {

    $timestamp= $feed->timestamp;
    $thedate= date("M. d, Y", $timestamp);
    $output .= '<p><big>' . l($feed->title, $feed->link) . '</big><br />'.$thedate .'</p>';
}
$output .= '</div>';
return $output;
?>

Here is a version that outputs a link to the source of each item:

<?php
// Last update: April 23, 2008
$result = db_query("SELECT a.title, a.link, a.timestamp, a.fid
    FROM {aggregator_item} a
    ORDER BY a.timestamp DESC");
while ($feed = db_fetch_object($result)) {
    $timestamp= $feed->timestamp;
    $thedate= date("M. d, Y", $timestamp);
$value=$feed->fid;
$credit = db_query("SELECT u.fid, u.title, u.link
    FROM {aggregator_feed} u
    WHERE u.fid=$value");
($credit = db_fetch_object($credit));
$credit_title=$credit->title;
$credit_link=$credit->link;
$output .= '<p><big>' . l($feed->title, $feed->link) . '</big><br /><a href="'.$credit_link.'">'.$credit_title.'</a><br />'.$thedate .'<br />&nbsp;</p>';
}
return $output;
?>

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

Comments

marcvangend’s picture

If all you want is to add dates to the aggregrator block, there is a simpler way to do that. (No offense to the author of the code above, he had more problems to solve than just adding dates, and seems to have solved them well.)

Adding dates can be done by overriding the theme_aggregator_block_item function. (If needed, there are even more variables passed to the theme function.) I also added some divs and classes for easier styling. This is the code I use, except for the name of the function, which you should adapt to your own theme name.

<?php
/**
* override theme_aggregator_block_item
 */
function themename_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>';
    }
  }
 
  $output .= '<div class="feed-item">';
  $output .= '<div class="feed-field feed-field-date">'.date("d/m/y", $item->timestamp).'</div>';
  $output .= '<div class="feed-field feed-field-title"><a href="'. check_url($item->link) .'">'. check_plain($item->title) .'</a></div>';
  $output .= '</div>';
  
  return $output;
}
?>
bemckimmy’s picture

I have used the code you posted above to get dates to show up in the aggregrator block, but I would also like to add the teaser text to the block.

Is that possible, and if so, what is the code I would need to add?

Thanks,
Becky

marcvangend’s picture

To see exactly which variables are available for output, add this line to your theme function (and remove it when you're done):
print_r($item);
You'll see that the teaser is available as $item->description.

Nick Brown’s picture

Where about in a theme would I put this code?
As an example I'm using the arthemia theme.

lhtown’s picture

I just dropped this code in a node or block.

If you are using Drupal 6 you can do this (more elegantly) with views.

That is what I am doing at www.unpluggedshop.com on the front page.

lanexa’s picture

I was able to add pull only the latest headlines by adding LIMIT 5 to the SQL statement above like this

SELECT a.title, a.link, a.timestamp, a.fid
    FROM {aggregator_item} a
    ORDER BY a.timestamp DESC LIMIT 5

and now it's pulling only what I'm calling the "latest" headlines in my block. Just thought this might be helpful to someone else. There may be a more elegant way of doing this, but here is my solution.

Thanks for posting this.
mcspoon

disbag’s picture

this code work for me in drupal 7, I hope it helps you :)

function theme_aggregator_block_item($variables) {
	$item = $variables['item'];

	$output = '<span class=date-post>' .date('d.m.y.', $item->timestamp). '</span><span class=title-post><a target="_blank" href="' . check_url($variables['item']->link) . '">' . check_plain($variables['item']->title) . "</a></span>\n";
  
  return $output;
}