Adding a simple twitter feed to your Drupal site
I just added a simple twitter feed to one of my sites and thought I'd share what I did. The only other post I could find about was this one http://drupal.org/node/151185 and I didn't really think it should be necessary to write any code (and especially not any db queries) views could do all the display stuff I needed.
I wanted to display the results of a twitter search for wedful some like as follows:
This shouldn't be too hard, and as it turns out, it isn't :). The only tricky part was parsing the username out of the feed results that twitter provides. I used the views customfield module to write some basic PHP code to parse that out.
Enable the aggregator module and add a new category (admin/content/aggregator/add/category) called "twitter". Then add a new feed to that category. In my case the feed was http://search.twitter.com/search.atom?q=wedful. Add multiple feeds here if you want to bring in tweets from multiple search results or user accounts (or a combination of the two).
Create a view of type Aggregator item. Add the fields:
Aggregator: Link - Set to Exclude from Display
Customfield: PHP Code -- Add the following as the value:
$link = $data->aggregator_item_link;
$parts = split('/', str_replace('http://twitter.com/', '', $link));
return l('@' . $parts, 'http://twitter.com/' . $parts) . ':';
This simply parses the name out of the feed item link so that we can display the tweet author in a nice way.
Aggregator: Body - Select Rewrite the output of this field and add the following text:
[description_1] <small><a href="[link]" class="twitter-external-link">(link)</a></small>
This will add the original tweet and a little link back to it.
Add a filter to the view to filter on only items in the 'twitter' aggregator category and sort your feed items by Aggregator: timestamp' in descending order. Now you can add a display of type Block and you're good to go.
I decided not to use the twitter module for this because I don't think it's at all necessary. This just uses simple RSS feeds and can work with any microblog service in a similar way (you'd just need to make the custom php code more generic).
I've attached an exported view to use as an example. It requires the customfield and the core aggregator modules be installed on your site.