Created
July 30, 2014 07:56
-
-
Save msankhala/5c82581650f64cc6f4de to your computer and use it in GitHub Desktop.
Drupal’s hook system allows for modules to interact and alter the workings of other modules or even Drupal core itself. It is a very simple system that allows modules to even create their own very easily. In common practice, there are two types of hooks that you would want to create - alter hooks and intercepting hooks. Alter hooks provide a com…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example #1 (simple invoking) | |
<?php | |
// will call all modules implementing hook_hook_name | |
module_invoke_all('hook_name'); | |
?> | |
//Example #2 (aggregate results) | |
<?php | |
$result = array(); | |
foreach (module_implements('hook_name') as $module) { | |
// will call all modules implementing hook_hook_name and | |
// push the results onto the $result array | |
$result[] = module_invoke($module, 'hook_name'); | |
} | |
?> | |
//Example #3 (altering data using drupal_alter) | |
<?php | |
$data = array( | |
'key1' => 'value1', | |
'key2' => 'value2', | |
); | |
// will call all modules implementing hook_my_data_alter | |
drupal_alter('my_data', $data); | |
?> | |
//Example #4 (passing by reference cannot use module_invoke) | |
<?php | |
// @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); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment