Last updated August 15, 2013. Created on October 25, 2001.
Edited by LeeHunter, dash, xenophyle, Delfio2d2. Log in to edit this page.

A Drupal module is a collection of files containing some functionality and is 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 Drupal core. In fact, a module is no different from a regular PHP file that can be independently created and tested and then used to drive multiple functionalities.

This approach allows Drupal core to call at specific places certain functions defined in modules and enhance the functionality of core. The places where code can be executed are called "hooks" and are defined by a fixed interface.

About hooks

Hooks are how modules can interact with the core code of Drupal. They make it possible for a module to define new urls and pages within the site (hook_menu), to add content to pages (hook_block, hook_footer, etc.), to set up custom database tables (hook_schema), and more. This page lists the hooks provided in the core, but modules can define hooks of their own. For example the cck module defines hook_field_info, which can be used by modules that want to define a new type of content field. Most modules that define hooks will also provide documentation about them.

Hooks occur at various points in the thread of execution, where Drupal seeks contributions from all the enabled modules. For example, when a user visits a help page on a Drupal site, as Drupal builds the help page it will give each module a chance to contribute documentation about itself. It does this by scanning all the module code for functions that have the name mymodule_help($path, $arg), where "mymodule" is the module's name, e.g., the block module's help hook is called block_help and the node module's help hook is called node_help. The hook may provide parameters; hook_help's parameters $path and $arg allow the developer to determine what page or pages the help messages will appear on.

A hook can be thought of as an event listener in the sense that an event triggers an action. The event in Drupal, such as deleting a node, would trigger the hook "hook_delete". If your module implemented hook_delete, that function would run when a node deletion occurred. As an example, your function might be to decrease the count of the total number of nodes, so when a node was deleted, your function would be called and lower the count by 1.

See also the overview of module hooks, in the Drupal API Reference.

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

Comments

chrisshattuck’s picture

Not sure if this is the right place for it (feel free to delete the comment if it's not, or integrate it if it is), but I put together a brief video explaining why wrapping your code in a module is important. It might be helpful for folks getting started.

I've recorded over 1100 focused Drupal video tutorials at BuildAModule.

rhuffstedtler’s picture

None of the videos are playing for me.

fabius’s picture

As this is a would-be module programer's first contact, it might be a good idea to elaborate on the range of module funcionality (eg, ". . . from simple functions to complex modules with . . . . ". Give an idea of how big the sandbox is.

fabius

Fiable.biz’s picture

It should be made clear whether the hook is called instead of, before or after a core's function.

http://Fiable.biz Web site creation.