Twitter widget using the Computed Field CCK module

In an earlier article I described how we can create a drupal block to display a twitter profile widget.  This earlier method would use PHP in the block to evaluate the node each time and check if a twitter account existed.  It works great but there are other alternatives I wanted to explore.

Yesterday I started using the computed field module.  This is a CCK add-on module that executes PHP code during a node insert/update and (optionally) stores that value in the database.  I say optionally because I believe you can also set it to evaluate PHP on the fly, however for something like a twitter widget we don't need to do this dynamically.  Once the widget information is stored in the database we only need to display it.

So the objective here is to use the computed field to check if the node contains a twitter account.  If it does then we simply compute the widget code and save it.  If not, we leave it blank.  So let's get started.

A different twitter widget for each #drupal node

I'm putting together a site that has a content type called Company.  Each company may or may not have a twitter account where they broadcast updates.  The requirement is to display a twitter widget on each company node if one exists.  

My first thought was to search through the list of custom modules instead of trying to reinvent the wheel.  I found several twitter-related modules, however nothing seemed to fit the bill.  The closest module was the twitter module but that connects users accounts to their twitter accounts.  I need something to connect companies (as nodes).

Later through a google search I found that offers widgets, and specifically a profile widget.  The profile widget, which you can now see on the right side of this blog, provides the html/script code to place inside of your website to display the twitter feed of any account.  We can use this widget and create a drupal block with some PHP code to pull the twitter account from the company node and display this widget.