Create a live twitter feed aggregator module for Drupal 6 - Part 2

The next file we will look at is the .module (read dot module) file is where we get down to the php. This file contains a number of functions that will do all the work in creating the content we require. Many of the functions we will use are actually hook functions that drupal calls at specific times during page rendering process.

Starting the .module

According to drupal coding standards first thing we should be providing in this file is a RCS $Id$ keyword (c.f. drupal.org/node/546). This will allow drupal CVS to add some more useful information if/when it gets uploaded to the source repository.

Right after that should be the Doxygen documentation generation information (c.f. drupal.org/node/1354).

So before we have written anything we have some stuff in our livetwitterfeed.module file already has some lines in it:


// $Id$
/**
* @file
* A Module that reads data from Twitter.
*
* This module will output a feed to a block from twitter.com
* @see http://www.twitter.com
*/

hook_help function

The next thing I like to do is implement the hook_help function. The Drupal API provides numerous functions that we will be using to build up the module and the help function gives our users more information about the functions we are providing. Follow this link for a drupal hook function cheatsheet.

To implement any of drupal's hook functions all we have to do is replace the word hook with our machine readable module name, then the name of the function, we want to implement, so hook_help becomes livetwitterfeed_help(), and we add the following to our livetwitterfeed.module file. (Notice each function has its own documentation block, that should also be included)

/**
* Display help and module information
* @param path which path of the site we're displaying help
* @param arg array that holds the current path as would be returned from arg() function
* @return help text for the path
*/
function livetwitterfeed_help($path, $arg) {
  $output = ''; //declare your output variable
  switch ($path) {
    case "admin/help#livetwitterfeed":
      $output = '<p>'. t("Creates a block with content fed from Twitter.com") .'</p>';
      break;
  }
  return $output;
} // function livetwitterfeed_help

hook_perm function

The next function we going to implement is the permissions function. In more complex modules we can restrict all basic CRUD operations, as well as limiting these abilities to owners of the content or give all permissions. The hook_perm function is where we define the permissions that can be set.

The accepted naming convention for permissions is "action_verb modulename". So we will restrict who can create feeds, administer feeds and access feeds, by adding the following

/**
* Valid permissions for this module
* @return array An array of valid permissions for the livetwitterfeed module
*/
function livetwitterfeed_perm() {
  return array('access livetwitterfeed', 'create livetwitterfeed', 'administer livetwitterfeed');
} // function livetwitterfeed_perm()

In the next part, we'll be adding the block function, which will control the output of the block as well as the administration of the block content.