Drupal - What is the basic concept behind hooks?

The other answers are great, accurate, detailed, but I'm not sure they're the "simple words" explaining the bare bones of the concept that the asker was looking for.

I think of hooks as a point where the code pauses and shouts "Anyone else got anything to add here?". Any module can have a function that replies to this, and gets triggered with appropriate data passed to it at that point in the code.

A nice straightforward example is hook_node_delete(). Any module can use it to make things happen every time a node is deleted. The docs tell you this hook passes the module the object of that deleted node to work with, and outlines other useful information such as about the exact timing of when it's called (e.g. that it is before the node data is actually deleted from the database), and where in Drupal's code the hook is called (which can be more than one place).

You can explore what hooks exist and find out what data is passed to them by exploring things starting with "hook_" in the Drupal api.

Hooks work by a name conventions: using hook_node_delete as our example, when the node deletion process reaches the point where the hook is called, for every module with a function like this [modulename]_node_delete() where the word hook in the hook's name is replaced with the module's name (e.g. my_amazing_module_node_delete() ), those functions get called.

Why? So any module can do anything at these key points: for example you could look at the deleted node and do things if it meets a certain condition (say, email an administrator, or launch some long process).

Some hooks let you alter things that have been generated just before they're processed. For example, hook_menu_alter() passes you the current menu items that the system has generated. Any module can define a function some_modulename_menu_alter() and look at them, optionally change them (delete some, add some, sort them...), and pass the newly altered menu back in.

It's simple, really powerful and is at the heart of how Drupal works as a modular system. Implementations of hooks are at the heart of most Drupal modules.

When looking through the code of a Drupal module, you can spot which functions come from hooks (as opposed to functions that are simply called from within the module code itself), as the Drupal community enforce a convention whereby each implementation of a hook has a comment in front of it like this (note the "Implements hook_..." bit):

/**
 * Implements hook_some_hook().
 *
 * Some descriptive summary of what this does
 */
function my_amazing_module_some_hook() {

Some modules that act as APIs define their own hooks. For example, Views defines many hooks that allow you to add to, read and edit data at various points in the process of creating or displaying a view. You can find information about hooks created in custom modules from two places (assuming the module follows conventions etc):

  • The code and comments in the modulename.api.php file in the module folder
  • drupalcontrib.org - for example, here's their list of D7 modules they have info on, and here's their page of Views hooks

Bootstrapping is, as others explained, basically booting up - I won't duplicate the other good clear explanations.


Hooks, are mostly implementations of the Visitor, and Observer patterns.

One of the most common hooks implementations is hook_menu, which allows modules to register new paths within a Drupal system.

function my_module_menu() {
  return array('myawesomefrontpage' => array(
    'page callback' => 'function_that_will_render_frontpage'
  ));
}

A very frequent pattern in Drupal is having a [DATATYPE]_info hook, and a [DATATYPE]_info_alter hook. If you want to create a new field type, you will implement the relevant field_info-hook, and if you want to manipulate an existing one, you will implement the corresponding field_info_alter-hook.

Edit: As Chx's points out in the comments, the observer pattern is object oriented, which Drupal 7 is still, mostly, not. There is however a wiki page, Drupal programming from an object-oriented perspective (Created by JonBob on April 4, 2005), that explains how Drupal uses object oriented code patterns despite this. It's interesting to note that it mentions observers, but not visitors.

Note on Drupal 8 This is still rather early, and is subject to change, but I want to add that while hooks have for quite some time been the de-facto standard in adding functionality to Drupal, the concept of plugins will become much more visible in Drupal 8, and will give us new ways of interacting with Core. Relevant issue, and documentation.


In layman's terms, hooks are sort of bridges which provide a way for modules to interact with each other, alter each other's structure and data, provide new data etc.

In most cases, the word hook_ in function names is replaced by the name of your module, and that provides a way for your module to tap into another module's operation. For example a drupal core module called "node" invokes various hooks. One of them is hook_node_update which gets invoked everytime an existing node is updated. When this hook is invoked, your module's (say we call it mymodule) implementation of hook_node_update is called, which in this case will be a function in your module's .module file called mymodule_node_update (Obviously this function can be in any file in your module's folder as long as it's included in the .module file as well). This hook will also be passed the necessary parameters (variables) which it can use, modify and/or return back to the function that invoked the hook.

When I first started learning Drupal, I was in the same boat as you are now, it's bit tricky to grasp at first, but once you get it, it's oh so simple and intuitive. Good luck.