Adding a simple twitter feed to your Drupal site

10 comments

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:

@username: this is a tweet about wedful.com [link]

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:

<?php
$link = $data->aggregator_item_link;
$parts = split('/', str_replace('http://twitter.com/', '', $link));
return l('@' . $parts[0], 'http://twitter.com/' . $parts[0]) . ':';
?>

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.

Peanut Gallery

Twitter Pull

There is also http://drupal.org/project/twitter_pull, which is mainly convenience functions in PHP.

Reply to comment | Scott Hadfield

gewinnspiele

I am genuinely thankful to the holder of this web site who
has shared this wonderful article at here.

Thanks Boris, I hadn't seen

Scott Hadfield

Thanks Boris, I hadn't seen that one. I also updated your link with the _ instead of the -

Thanks

No attachment? Oh, and also, turning this into a Feature instead and sticking it on Github would be cool. Although, not sure if the aggregator module is exportable (I don't think it is).

whoops :) bunk permissions on

Scott Hadfield

whoops :) bunk permissions on the upload viewing. Yeah, I thought about doing this as a feature with a bit more options. And I don't think it would take a much work to make the aggregator module exportable either, not a lot of settings there (but that would be for another day). In the end pretty much all the work was in the view, so it seemed to be simplest just to post that.

Thanks!

I was trying out the Twitter_Pull project but this makes it a lot easier to customize. Thanks for the awesome post!

Good Stuff...But I Can't Get it Working

Summitt Dweller

I agree with Broda...Twitter Pull is quick and simple but I like the flexibility of pulling tweets in as content so that I can work with them using Views.

Problem is, I can't seem to get the aggregator to pull the content I want. What's the proper URL form to pull the tweets for a specific user...like say me (@SummittDweller)?

Drupal 7.0

HI,
I can't find a way to create a category. I'm using Drupal 7.12.
Does it work on this version
The link admin/content/aggregator/add/category doesn't show me a CATEGORY to create.
Thanks.

Great stuff.....was looking

pet transport

Great stuff.....was looking for something like this to work. I am having fun working with it.

I agree with Broda...Twitter

I agree with Broda...Twitter Pull is quick and simple but I like the flexibility of pulling tweets in as content so that I can work with them using Views.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options