OK

Drupal Aggregator Timestamps and Views Ordering

Drupal - Aggregator FixDrupal’s core Aggregator module is great. It’ll handle pretty much any type of feed you throw at it, store the contents locally, and let you display it all however you want.

I recently began using it at work to display some partner feeds on latina.com, but ran in to a small problem – the feed items were not being ordered by their associated timestamps. Even worse, they looked like they weren’t updating at all. This problem didn’t occur when working just with the Aggregator module, but when I used the Views module to display the feed items. Output from the Aggregator module alone worked as expected.

It turns out that, for one reason or another, Drupal was not importing the feed item’s timestamps along with everything else. It was using the time the feed was imported at instead. That doesn’t really help when you want to display the 5 newest feed items.

Views allows for sorting, but there are no fields available to sort by that would solve the problem. There is however an IID number assigned to each feed item as it’s inserted in to the database which could be used, if it was accessible by Views.

Solution

The solution was to modify the View with a preprocess function that altered the SQL query used in order to sort the results properly. The function swaps out “aggregator_item_timestamp” with “aggregator_item_iid”. All you need to do is insert the function below in to your template.php file after making a few small changes:

  1. Go to your View’s edit page and make sure that it’s being sorted by timestamp in descending order.
  2. Click the “live preview” button and copy the SQL query that is generated.
  3. Replace “VIEW_NAME_HERE” with your View’s name and “SQL_QUERY_HERE” with the copied SQL Query (after replacing “aggregator_item_timestamp” with “aggregator_item_iid”).
function views_views_pre_execute(&$view) {
if($view->name=="VIEW_NAME_HERE") {
		$view->build_info['query']="SQL_QUERY_HERE";
	}
}

Copy the template.php file back in to your theme’s folder, rebuild the theme registry,  and you’re all set! You could also build this as a module so that it isn’t limited to only the theme that is currently in use. Simply create a module as you normally would and change the function name to MODULE_NAME_views_pre_execute.

For Your Reading Pleasure...

post a comment
The follwing HTML tags are allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Search

Post Details and Sharing

December 2010
SMTWTFS
« Aug Jan »
 1234
567891011
12131415161718
19202122232425
262728293031 

Twitter @senorfox

@solothedrunk BRILLIANT!
tweeted 9 days ago.
RT @AtTheDriveIn_: ¡ ATTENTION ! To whom it may concern: AT THE DRIVE-IN will be breaking their 11 year silence THIS STATION IS …NOW…OPE ...
tweeted 27 days ago.
Can one ever have enough @KrunchPizza? NO. http://t.co/AlVgLB8q
tweeted 45 days ago.
My Bawls are showing. http://t.co/cQjHSRIh
tweeted 58 days ago.
@KrunchPizza Best BBQ chicken pizza. Ever. http://t.co/nkAbCmPH
tweeted 101 days ago.

Last.fm Playlist

Public Menace, Freak, Human Fly
Powerman 5000
scrobbed 7 days ago.
White's Ferry
Clutch
scrobbed 8 days ago.
The Devil & Me
Clutch
scrobbed 8 days ago.
Power Player
Clutch
scrobbed 8 days ago.
You Can't Stop Progress
Clutch
scrobbed 8 days ago.