Darren Mothersele

London based Drupal Web Developer
Facebook Twitter Drupal.org LinkedIn RSS Feed

Simple Twitter Block for Drupal

Here's a quick way to add recent tweets to your Drupal powered websites. The following code is as simple as possible. Add it to your site's module folder and enable the module. You they get a block to position on your site. The block is populated with your most recent tweets via a Javascript call to the Twitter website.

First thing to do is create the module info file so Drupal can find your module. I'm going to call the module tweetblock - but you can change this if you like. I put the following code in a file called "tweetblock.info"

name = Twitter Block
core = 6.x
package = Other

Then I create the main module in a file called "tweetblock.module" with the following code:

<?php
function tweetblock_block($op = 'list', $delta = 0, $edit = array()) {
  if (
$op == 'list') {
   
$blocks[0] = array(
     
'info' => t('Twitter'),
    );
    return
$blocks;
  }
  if (
$op == 'configure' && $delta == 0) {
   
$form['tweetblock_username'] = array(
     
'#type' => 'textfield',
     
'#title' => t('Twitter Username'),
     
'#default_value' => variable_get('tweetblock_username', 'mothersele'),
    );
   
$form['tweetblock_count'] = array(
     
'#type' => 'textfield',
     
'#title' => t('Number of items'),
     
'#default_value' => variable_get('tweetblock_count', 6),
    );
    return
$form;
  }
  if (
$op == 'save' && $delta == 0) {
   
variable_set('tweetblock_username', $edit['tweetblock_username']);
   
variable_set('tweetblock_count', $edit['tweetblock_count']);
  }
  if (
$op == 'view' && $delta == 0) {
   
$tweetcode = "";
   
drupal_add_js($tweetcode,'inline','footer');
   
$block = array(
     
'subject' => t('Twitter'),
     
'content' => "<div id=\"twitter_div\"><ul id=\"twitter_update_list\"><li>...loading...</li></ul></div>",
    );
    return
$block;
  }
}


function
tweetblock_footer($main = 0) {
 
$username = variable_get('tweetblock_username', 'mothersele');
 
$count = variable_get('tweetblock_count', 6);
  return
"<script type=\"text/javascript\" src=\"http://twitter.com/javascripts/blogger.js\"></script><script type=\"text/javascript\" src=\"http://twitter.com/statuses/user_timeline/{$username}.json?callback=twitterCallback2&amp;count={$count}\"></script>";
}
?>

That's just two functions. The first defines a Drupal block called "Twitter" which will contain your recent twitter updates. It defines two new block settings, one for your twitter username, the other for the number of tweets to show.

The second function adds the Javascript call to the footer of your page. This means your page loads completely before the tweets are fetched. The javascript call requests your recent tweets from the twitter server and adds them to the block created by the first function.

Oops

Did exactly what you said, but after enabling this in 6.10 it made the whole site break and go blank. Had to backup to an older version of the DB.

Thoughts?

If the screen goes blank then

If the screen goes blank then that indicates a PHP error. Check the error log on the server for the exact error, or you can make the site show the error message by temporarily adding this to the top of the settings.php file:

<?php
error_reporting
(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
?>

It's probably just a syntax error, missing ';' or something like that.

Thanks, I get the following

Thanks, I get the following now:

warning: Cannot modify header information - headers already sent by (output started at /Users/dfiller/Sites/drupal/sites/default/settings.php:7) in /Users/dfiller/Sites/drupal/includes/common.inc on line 141.

There's something being

There's something being output from settings.php. Check that it has a PHP opening tag at the start of the file, and no other PHP tags.

I.e. The file starts with <?php and this appears only once in the file.

the way i use twitter: let my

the way i use twitter: let my drupal age send me direct mails on news comments. not a module yet, but some php to copy and paste:

http://www.lopsta.com/en/story/send-yourself-a-direct-tweet-new-comments...

great idea

That's a great idea. Thanks for sharing.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • 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 may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

Recent comments

Latest Posts