Making the Drupal Module Work

Following up on my last post about Writing a Simple Drupal Module, I thought I'd add some details about making it an actual module and making it a usable module. Where I left off last time, I actually had to go into the database and manually insert feeds to process. Drupal used to have a bit of a reputation for difficult administration, but that's a bit much.

There's a few more details I should cover:

  • making the module installable
  • running the task on schedule
  • administration screens

Making an installable module

To make it an installable module, there's two files at a minimum that are required: {modulename}.module containing the main code for the module and {modulename}.info that gives Drupal some basic information about your module.

The .module file (I named it tweetrss.module) contains the code from my last post as well as a few other bits. The file is as follows:


; $Id$

name = Tweet RSS
description = Process an RSS feed and post to Twitter.
dependencies[] = twitter
dependencies[] = aggregator
core = 6.x

Basically, that's the name, description, module dependencies and the version of Drupal it works with. There's a few other details as well, like registering a menu entry for the admin screens and setting up administration permissions, but this is the basics.

Making it run on schedule

To get the process to run on schedule, I needed two things: write for hook_cron() and ensure that cron is being called on a regular basis. The hook_cron() function is something that Drupal will call automatically whenever the cron process runs. So my function will be named tweetrss_cron() (ie. {modulename}_cron()) and is roughly as follows:

function tweetrss_cron() {
  module_load_include('inc', 'twitter');
  $results = db_query("... select stuff from the `aggregator_item` and `twitter_account` tables...");
  while ($row = db_fetch_array($results)) {
      $tuid = $row['twitter_uid'];
      $tweet = $row['title'];
      tweetrss_tweet($tuid, $tweet);
      tweetrss_check_row($row); //mark the RSS item as read

Note: in any production use, you will need a real cron process to call Drupal's cron. Poormanscron () will work for a proof of concept, but see: for more details on cron.

Administration screens

It seems sort of odd that the main functioning part of the module can be written in a few lines of code, while the screens to administer it take much longer. Developing the admin screens relies on the Drupal forms API and while relatively simple, still takes some fiddling to get it do do what you want. Maybe the Form API is a good subject for another post, but I'll just include a couple of screenshots for now...

TweetRSS Feed List ScreenTweetRSS Feed List Screen

TweetRSS Feed Detail ScreenTweetRSS Feed Detail Screen