Creating Drupal 7 hooks

Last updated on
12 November 2017

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

The Drupal 7 Hook Concept

Drupal’s hook system allows modules to interact with and alter data of other modules, or even Drupal core itself.

To use a hook system you must create the hook and call their implementations.

After reading this page you will have a solid understanding of the concept of hook, and will have seen a few basic examples.

Creating a Drupal 7 hook (and calling their implementations)

Creating a hook and calling their implementations can be done by using one of the following functions (not all included): drupal_alter(), module_invoke_all() and module_invoke()

Implementing a Drupal 7 hook

To be able to call it you need to implement a drupal 7 hook.

When calling a hook, "hook" is always replaced with the name of your module. For example, in test.module, hook_help() would be defined as test_help().

Drupal 7 Hook Types:

In common practice, there are two types of hooks that you can create:

  1. Alter hooks:
    a common way to edit the contents of a particular object or variable by getting variables in the hook by reference, typically by using drupal_alter()
  2. Intercepting hooks:
    allowing external modules to perform actions during the execution, can not get variables by reference, typically by using module_invoke_all() and module_invoke()

Examples:

Example #1 (Simple Invoking)

  // Calling all modules implementing 'hook_name':
  module_invoke_all('hook_name');

Example #2 (Invoking a Particular one)

  // Calling a particular module's 'hook_name' implementation:
  module_invoke('module_name', 'hook_name');
  // @note: module_name comes without '.module'
  // @note: hook_name is the specific hook defined in that module 

Example #3 (Collecting results in an array)

  $result = array();
  foreach (module_implements('hook_name') as $module) {
    // Calling all modules implementing hook_hook_name and 
    // Returning results than pushing them into the $result array:
    $result[] = module_invoke($module, 'hook_name');
  }

Example #4 (Altering data using drupal_alter())

  $data = array(
    'key1' => 'value1',
    'key2' => 'value2',
  );
  // Calling all modules implementing hook_my_data_alter():
  drupal_alter('my_data', $data);
  // You should implement hook_my_data_alter() in all other modules in which you want to alter $data

Example #5 (passing by reference: cannot use module_invoke())

  // @see user_module_invoke()
  foreach (module_implements('hook_name') as $module) {
    $function = $module . '_hook_name';
    // will call all modules implementing hook_hook_name
    // and can pass each argument as reference determined
    // by the function declaration
    $function($arg1, $arg2);
  }

Help improve this page

Page status: No known problems

You can: