Create your own hooks for Drupal module development

Source: Internet
Author: User
Tags foreach drupal

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 );
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.