Drupal Modules

Introduction

A Drupal module is simply a collection of files containing a set of routines written in PHP. Because the module code executes within the context of the site, it can use all the functions and access all variables and structures of the Drupal Core|main engine.

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

  1. /sites/all/modules or
  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.

As of Drupal 6.x, sites/all/modules is the preferred place for non-core modules and sites/all/themes for non-core themes.

Module files begin with the opening PHP tag <?php. Closing tag ?> is not needed.

Telling Drupal about your module

All modules must have a modulename.info file, which contains meta information about the module. Semicolons are used to comment out a line.

Format:

; $Id$
name = Module name
description = A description of what your module does.
core = 6.x

name

(required)
Human readable name.

description

(recommended)
Short description of the theme. Display at Administer > Site building > themes

screenshot

Where to find the theme's thumbnail image.

core

required
From 6.x all .info files must indicate what major version of Drupal core they are compatible with.

more about info fields
info fields for Drupal 5.x
Structure of the .info file

Hooks

Allow modules to interact with the Drupal core. A hook is a PHP function that is named foo_bar(), where "foo" is the name of the module (whose filename is thus foo.module) and "bar" is the name of the hook. Each hook has a defined set of parameters and a specified result type.

The most common way to tap into Drupal’s core functionality is through th implementation of hooks in modules.

hook_init

This hook is run at the beginning of the page request. It is typically used to set up global parameters which are needed later in the request. when this hook is called, all modules are already loaded in memory.

hook_node_info

This is a hook used by node modules. This hook is required for modules to define one or more node types. It is called to determine the names and the attributes of a module's node types.

hook_perm

This hook can supply permissions that the module defines, so that they can be selected on the user permissions page and used to grant or restrict access to actions the module performs.

hook_access

This hook allows node modules to limit access to the node types they define.

hook_schema

A Drupal schema is an array structure representing tables and their related keys and indexes. This hook must be in .install file.
Schema API

hook_help

Drupal's function hook_help() is used for help and additional information about the module.

hook_validate

Verifies a node editing form.

hook_perm Telling Drupal who can use your module

Drupal's hook_perm() specifies what permissions are available for a module. This function just needs to return a list of the permission names this module will use

Install Module

Files name.module and name.info have to be in the right directory on your site.

Module developer's guide

http://drupal.org/developing/modules

Links

Drupal Modules
Creating Drupal 5.x modules
Creating Drupal 6.x modules

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.