Drupal's hook system allows interaction with modules and changes the logic of other modules, or even changes the core logic of Drupal. This is a very simple system, and even allows third-party modules to create their own hooks. In general practice, there are two types of hooks you may want to create, one is the content modification class hook, the other is the interception class hook. The hook of the modify class provides a standard method to modify the content of a specific object or variable. Typically, the drupal_alter () function is used. The interception hook allows a third-party module to perform certain actions according to the conditions during Module execution.
Example 1: Simple call
The code is as follows: |
Copy code |
<? Php // Will call all modules implementing hook_hook_name Module_invoke_all ('hook _ name '); ?> |
Example 2: aggregation result
The code is as follows: |
Copy code |
<? 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: Use drupal_alter () to change the content
The code is as follows: |
Copy code |
<? Php $ Data = array ( 'Key1' => 'value1 ', 'Key2' => 'value2 ', ); // Will call all modules implementing hook_my_data_alter Drupal_alter ('My _ data', $ data ); ?>
|
Example 4: reference parameter passing. module_invoke cannot be used.
The code is as follows: |
Copy code |
<? 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 ); } ?> |