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. Only create this file if you need to do something when the module is installed or uninstalled. 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 variables which is defined in our module
  variable_del('customsite_setting1');
  variable_del('customsite_setting2');
}

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.

More references

You may find module_builder.module useful. It can create a skeleton module with these required files and hook declarations for you, based on templates. It does seem to require you to install drush first (but drush is a good idea anyway, so try that).
As well as the examples in the API reference, the Examples (module) may also contain useful things to learn from.

Comments

ahaapaka’s picture

Is this really needed? Doesn't Drupal automatically remove the module from the system table when module is uninstalled?

Berdir’s picture

It does.

As If’s picture

Yeah but if your module does any other stuff you want to clean up after - for instance if you stored anything in the variables table, or if you created any custom tables - you would delete them in this hook.

-------------------------------------------
Interactive Worlds and Immersive Obsessions
http://www.asifproductions.com

wojtha’s picture

I changed the uninstall example to something more useful - deleting variables which we defined in our custom module.

egarias’s picture

I can't find the way to dynaically include other .module
Please help
here is my .info

; $Id$ borme_boe.info,v 1.01 2011/02/05 03:51:56 egarias Exp $
name = "BORME y BOE"
description = "Integración de datos oficiales."
core = 7.x
package = ORCA

files[] = borme_boe.module
files[] = licitacion.module

version = "7.x-1.0-beta1"
core = "7.x"
project = "orca"
datestamp = "1294367749"

My licitacion.module wich is in the same directory is not loaded
here is the licitacion.module

<?php
// $Id: licitacion.module,v 1.00 2011/01/06 19:50:23 egarias Exp $

function tratar_licitacion($fichero) {
}

In the borme_boe.module I have a call to tratar_licitacion($fichero) but when executing I have

Fatal error: call to undefined function tratar_licitacion()

Please any clue?

Egarias
Turning opportunities into business

tumblingmug’s picture

Only files containing class definitions need to be listed in the .info file and will then be included on demand by Drupal. This does not apply to functions. Files containing functions need not to be listed inside the .info file and you have to include them manually, as in Drupal 6. If you do not so, called functions are not declared.

egarias’s picture

Many thanks

Egarias
Turning opportunities into business