Add new comment

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

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
17 + 2 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.