Creating custom hooks in Drupal?

1) Alter hooks
   a common way to edit the contents of a particular object or variable by settings     variables in the hook by reference, typically by using drupal_alter()

2) Intercepting hooks
  Allowing external modules to perform actions during the execution, cannot get variables by reference, typically by using module_invoke_all(), module_invoke()

a) Simple invoking:
---------------------------
module_invoke_all() => Calling all modules implementing the hook

b) Invoking a particular one:
---------------------------------------
module_invoke('module_name', 'hook_name', 'arguments');
Function calls a particular hook for a particular module

c) Collecting results in an array:
---------------------------------------------
  $results = array();
  foreach(module_implements('hook_name') as $module) {
    $result[] = module_invoke($module,'hook_name');
  }

d) Altering the data using drupal_alter()
---------------------------------------------------------
drupal_alter() function => the first one is the name of the hook we want to invoke and the second is the data we want to alter

function example_list() {
  $data = array('name' => t('test'));
  drupal_alter('my_hook', $data);
}

function mymodule_my_hook_alter(&$data) { }

e) Passing by reference: cannot use module_invoke():
----------------------------------------------------------------------------
  foreach(module_implements('hook_name') as $module) {
    $function = $module.'_hook_name';
    $function($arg1, $arg2);
 }

function mymodule_hook_name(&$arg1, $arg2) {
}
 

Tags