How to create a simple Drupal 6 module in three easy steps

Last modified: September 4, 2009 - 23:40

Many Drupal developers and themers create one or more small custom modules to paste or write various code snippets into - while many aspects of Drupal can be changed at the theme layer (e.g. in .tpl.php files or in template.php), some code only works from within a module.

Though you should read the full Module developer's guide and particularly the step by step tutorial on creating a Drupal module, this short guide will help you create the framework for a simple personal module by simply creating a few files and pasting in the example code. You can have your own personal module, even if you're new to Drupal and/or unfamiliar with programming, within a matter of minutes.

While you can name your module anything you like (so long it's a unique name that won't clash with other existing core or contributed modules), it's useful to name this module something generic such as customsite or something along those lines (rather than a site-specific name), as you can then easily drop the module into any future Drupal sites you work on, without needing to adjust any names or code. If your module has a single specific purpose, you could name it according to that purpose.

Where to save your module

(More details: 01. Getting Started)

Your module will function in any location where Drupal looks for modules (such as the recommended location of sites/all/modules), however you may prefer to keep your custom module(s) organized separately from contributed modules. If so, make another sub-folder within sites/all/modules called custom (or your preferred name). Alternately you could place your site-specific custom modules at sites/default/modules (or sites/www.example.com/modules if you use multi-sites).

For this guide, the example will assume the location of sites/all/modules/custom. Within the "custom" directory, add a new folder called customsite which is where you will save the following 3 files. So in the example, the directory to save your files in is: sites/all/modules/custom/customsite

Make customsite.info

(More details: 02. Telling Drupal about your module, also see Writing .info files)

Make a file called customsite.info and paste in the following:

; $Id$
name = Custom Site functions
description = Custom functions for this site.
core = 6.x

Make customsite.install

(More details: Writing .install files)

Unlike the .info and .module file, the .install file is not required for Drupal modules. Make a file called customsite.install and paste in the following code (note that there should not be a closing PHP tag):

<?php
// $Id: customsite.install

/**
* Implementation of hook_install()
*/
function customsite_install() {
  // Set the module weight so it can override other modules.
  db_query("UPDATE {system} SET weight = 99 WHERE name = 'customsite'");
}

/**
* Implementation of hook_uninstall()
*/
function customsite_uninstall() {
  // Remove the module from the system table
  db_query("DELETE FROM {system} WHERE name = 'customsite'");
}

Make customsite.module

(More details: bottom of 02. Telling Drupal about your module and the following pages of the Creating modules guide)

Make a file called customsite.module and paste in the following code (note that there should not be a closing PHP tag):

<?php
// $Id: customsite.module

/**
* @file
* Custom functions for this site.
*/

Your custom code and snippets for the module will go below these identification comments in the file.

Enable your module

After saving the 3 files in the customsite folder, go to Administer > Site building > Modules (admin/build/modules) and enable the new "Custom Site functions" module that you've created. It doesn't do anything yet, but is now ready for you to begin writing or pasting code snippets. Functions in your module will be in the form of customsite_function()

As mentioned earlier, this lesson is meant to get you up and running with a generic personal module as quickly and easily as possible. However, please read the full Module developer's guide to learn how to proceed from this point.

 
 

Drupal is a registered trademark of Dries Buytaert.