Drupal really likes Twitter

When I was creating new website, I indeed wanted to keep all useful functions I had on my old site. One of them was list of latest comments from Twitter.

Have you ever noticed that almost all computer related problems have at least two solutions, “the clean and nice” one and “the dirty” one. In this case, the clean solution is to use Drupal module called Twitter, and the dirty one is creating new block that includes raw PHP code. After long and painful consideration, I decided to choose the dirty one. Let me show you how I solved the problem.

First, you need to enable Aggregator module, which will periodically download RSS feed with your comments from Twitter. You can do that at /admin/build/modules page, Core - optional category. And when you're there, enable also PHP filter module, you'll need it later. After that, go to /admin/content/aggregator/add/feed page and add your RSS feed there. You can find out URL of your feed by looking at source code of your Twitter profile page.

Second (and also last) step is to create the block itself and you can do that at /admin/build/block/add page. Into “Block body” field, enter this code:

<?php
$fid = 1;
$limit = 5;
 
$result = db_query('SELECT description, timestamp FROM {aggregator_item} WHERE fid = '. $fid .' ORDER BY timestamp DESC LIMIT '. $limit);
 
print '<div class="item-list"><ul>';
while ($row = db_fetch_object($result)) {
  $text = $row->description;
  $nick = explode(":", $text);
  $nick = $nick[0];
  $nicklen = strlen($nick) + 2; 
  $text = substr($text, $nicklen);
  $text = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $text);
  $text = preg_replace('@\@([\w]+)@', '<a href="http://twitter.com/$1">$1</a>', $text);
  print '<li><div class="my-twitter-status">';
  if (($text[0] != '<') && ($text[0] == strtolower($text[0]))) {
    print $nick . ' ';
  }
  print $text . '</div>';
  print '<div class="my-twitter-status-time">' . twitter_ago($row->timestamp) . '</div></li>';
}
print '</ul></div>';
 
function twitter_ago ($timestamp) {
   $difference = time() - $timestamp;
   $text = format_interval($difference, 2) . " ago";
   return $text;
}
?>

Yeah, it's really ugly code, I know. But it's surely better than the version I wanted to publish few minutes ago. If IKEA was making software, it'd look exactly like that version - you'd have to finish it by yourself.

If you want to adjust the number of comments, feel free to change value of $limit variable. You probably won't have to change value of $fid variable if your Twitter RSS feed is first feed you added to Aggregator module.

Don't forget to change block input format to “PHP code”, otherwise it won't work at all. And that's it, everything should work when you save your block. Should.

Comments

Anonymous's picture

Adult shops are a great set

Adult shops are a great set out for unwilling man's victims to fall prey to these three hardcore femmes, who upon to tinker with flirt with with their man's customers by showing them their goods, but lone letting them look...not touch. Then they pillage these guys down and modify them ballad down on the off the track hard beat while they have his penis in ill-bred CFNM undertaking! Recognize the full-length incident phonerotica.cc.
phonerotica image

Anonymous's picture

Adult shops are a great set

Adult shops are a great set out for unwilling man's victims to fall prey to these three hardcore femmes, who upon to tinker with flirt with with their man's customers by showing them their goods, but lone letting them look...not touch. Then they pillage these guys down and modify them ballad down on the off the track hard beat while they have his penis in ill-bred CFNM undertaking! Recognize the full-length incident phonerotica.cc.
phonerotica image

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account, used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]".

More information about formatting options