Last updated 10 June 2009. Created on 7 September 2006.
Edited by jhodgdon, Steve Dondley, rszrama, add1sun. Log in to edit this page.

To focus this tutorial, we'll start by creating a block module that lists links to content such as blog entries or forum discussions that were created one week ago. The full tutorial will teach us how to create block content, write links, and retrieve information from Drupal nodes.

Where to place module files
As of Drupal 5.x, module files should be installed in one of two locations:

  1. /sites/all/modules
  2. if you run in a multisite configuration, you may want to install the module in a site specific modules directory (e.g. sites/yoursite.com/modules). This way, you can make modules available only for the sites that need them.

If the modules directory doesn't exist, you must create it. Make sure the permissions on the directory are set properly for your server configuration. Within the modules directory, you should create one new directory for each module.

Module files
Within individual module directories, you are required to have at least two files, one with a ".info" extension and one with a ".module" extension. The .info file is a simple text file that give Drupal core needed information about your module. The .module contains your php code that the Drupal core will load and execute.

As per the Coding standards, do not put a closing ?> tag at the end of your .module file and use the longhand <?php tag. (Note that the examples in the handbook will show the closing tag for formatting reasons only and you should not include it in your real code.)

One of the powerful features of the Drupal architecture are the specially named functions that allow your module to interact with the Drupal core. These special functions are referred to as "hooks" because they let your module hook into the rest of the Drupal core. The naming convention for these hooks is {modulename}_{hook}, where {modulename} is the name of your module and {hook} is a predefined string. For example, if you had a module named "foo" and you wanted to place links into Drupal's menu system, you would create a function called "foo_menu". All of Drupal's hooks are fully documented at http://api.drupal.org/api/group/hooks/5.

Modules committed to CVS will contain an $ID$ string to track the the revision number and date when the files was committed.

A skeleton of a Drupal module looks something like this:

<?php
// $Id$

/**
 * @file
 * Short description of the module here.
 */

// You can define constants here

// You can place Drupal hook functions here

// You can place your module-specific functions here

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.