What is Drupal Hooks and how to invoke hooks

Drupal Hooks are the beauty of Drupal . It provides easy way to enhance existing functionality of any drupal module.

For example: Suppose you are using any module say “ed_classified” Module to create your classified based site and you want a require as soon as someone post a classified then admin should get notified for same.

If we analyze the requirement then we can easily see that there are two different modules needs to interact together.

  1. As soon as someone add a new classified entry. This will be handled by ed_classiffied module.
  2. An email should get fired to admin this will be handled by user module.

So idea of hook is that intervene in existing flow of one module without direct tempering of that module. We can create separate function that will perform new task and only this function will get called through hooks. Calling of function (hooks ) are known Hook Invoking .

How does any module invoke hook function of other module:

In any module there is always a .module file it contain all important function related to configuration and might be some function might contain process logic as well. You can easily search “module_invoke_all” into this file. If this file contain any “module_invoke_all” function then it means this module has already calling hooks through this function. If any module_invoke_all called from one module then hook function implementation from all modules throughout the website will get called automatically.

Syntax to call hook through any module in Drupal 7

module_invoke_all(‘myhook_name’,$hook);

module_invoke_all has two argument

Aruguement 1: Hook name :this is the function name that will implement hooks functionality.

Argument 2: $hook: it contain an parameter that will pass to the hook.

Return Value

It provides return value of hook implementation. Even in case of multiple array it should be merged into single one array only.

Prectical example for developers

Cutehits.module file might contain a function Like


/**

* List of fruit.

*/

function cutehits_all_fruits() {

$fruits = array(‘apple’, 'banana', 'mango');

$fruits_more = module_invoke_all('cutehits_addhook');

$fruit_all = array_merge($fruits, $fruits_more);

$output = theme('item_list', array('items' =>  $fruit_all));

return $output;

}

 

Now drupal will check each and every module of that if there is some module that contain function named “cutehits_addhook” in any other .module file. If found then that function will also get called from it.

 

Suppose drupal found any other test module which contain this function then it will get defined like below


/**

* Implements test_ cutehits_addhook();

*/

function test_ cutehits_addhook () {

return 'Graves';

}

Now this function will get called from that module. Hope you understood the concept now.

The following two tabs change content below.

Chandra Shekhar

GCP Architect
Chandra Shekhar Pandey is Google certified Cloud engineer, I am Magento2 Trained developer. Having huge experience in designing cloud solution. I have around 12 years of experience with world enterprise IT companies and fortune 500 clients. During my architecture design I am always caring about high availability, fast performance and resilient system. From the programmer background I have huge experience in LAMP stack as well. Throughout my carrier I have worked on Retail, E-Learning, Video Conferencing and social media domain. The motive of creating cutehits was just to share the knowledge/solutions I get to know during my day to day life so that if possible I can help someone for same problems/solutions. CuteHits.com is a really a very effort for sharing knowledge to rest of the world. For any query/suggestion about same you can contact me on below details:- Email: shekharmca2005 at gmail.com Phone: +91-9560201363

Latest posts by Chandra Shekhar (see all)

You may also like...