Drupal - Create a Custom Hook for Other Modules to Use
In this example, we'll explore how to make a custom hook with our module so that other modules may use it.
For example, say we were implementing hook_entity_delete() to display a custom message whenever an entity was deleted. But before we display the message, we want to allow other modules to modify the message if they want. We would do that by 'creating' a custom hook by using module_invoke_all(), like so:
In the code example below, we'll 'create' a hook called: hook_change_my_entity_delete_message()
/** * Implements hook_entity_delete(). */ function MY_MODULE_entity_delete($entity, $type) { // Create our message variables. $variables = array('msg' => 'foo'); // Make sure at least one module implements our hook. if (sizeof(module_implements('change_my_entity_delete_message')) > 0) { // Call all modules that implement the hook, and let them make changes to $variables. $variables = module_invoke_all('change_my_entity_delete_message', $variables); } // Display the message. drupal_set_message($variables['msg']); }
Now if another module wanted to implement this hook and change the message, they could do that like so:
/** * Implements hook_change_my_entity_delete_message(). */ function SOME_OTHER_MODULE_change_my_entity_delete_message ($variables) { $variables['msg'] = 'bar'; return $variables; }
Now when an entity is deleted, our implementation of hook_entity_delete will get called and get ready to display the message, then call any module that implements hook_change_my_entity_delete_message(), then return and print out $variables['msg'].
Which in this case the drupal_set_message would display: bar
Comments
jason.xie@victh... (not verified)
Wed, 04/03/2013 - 07:47
Permalink
you can use drupal_alter()
you can use drupal_alter() too for altering purposes
tyler
Wed, 04/03/2013 - 09:46
Permalink
Hi Jason,Thank you for
Hi Jason,
Thank you for pointing out drupal_alter(). Now I feel like I should have used that function instead, and will look to that in the future as it seems it could provide a cleaner approach than what is implemented in this post.
Chris Gillis (not verified)
Mon, 08/12/2013 - 00:42
Permalink
Thanks Tyler,
Thanks Tyler,
Very helpful. A lot simpler than I thought it was going to be. The documentation for drupal_alter seems to be rather obtuse. If you do a revised blog post using that method let me know. :)
tyler
Mon, 08/12/2013 - 01:03
Permalink
Thanks Chris. I actually just
Thanks Chris. I actually just used drupal_alter() for the first time the other day.
From the code above, this line here would replace the line with the call to module_invoke_all()
Then any implementers of the hook would use a function template like this:
That should work! (didn't test it though)
mgoubert (not verified)
Mon, 03/17/2014 - 12:16
Permalink
hook_change_my_entity_delete
You forget the _alter :).
Bruce C (not verified)
Wed, 10/23/2013 - 11:50
Permalink
Tyler,
Tyler,
Great blog post and I think drupal_alter() is great, but you also could make it even simpler by using a regular variable without the $variables array and just passing a non-array variable with a reference:
Instead of $variables = array('msg' => 'foo'); you could simply use $message="foo"
Then, when the hook is implemented, it could use:
/**
What you did works as well. Either way would work.
BC
Sceptical (not verified)
Wed, 01/06/2016 - 00:43
Permalink
I wanted to add JS variables
I wanted to add JS variables from a number of modules and consolidate them as script using drupal_add_html_head(). I was running my function if a static variable wasn't true, to avoid calling the hook time and again.
However, I find modules aren't loaded, so in order to use module_invoke_all, or module_implements, I had to use module_load_all() first.
Seems like this is prolly real clumsy. I did try using:
if (drupal_get_bootstrap_phase() != DRUPAL_BOOTSTRAP_FULL) { return; }
But still, there was only a single module loaded that called the hook as before, so I assume I was in full bootstrap already: Assume it is related to module weights and/or dependencies.
I could set a variable or cache a file to disc or hardcode a config var, and then just load the modules calling the hook. First thought I would mention it in case someone had done it and had some added insight. Thanks in advance!
CodeNext (not verified)
Tue, 07/12/2016 - 22:13
Permalink
In the following code, I did
In the following code, I did not understant oen thing. How module_implements() function will determine how many modules
implemets the 'chnage_my_entity_delete_message' hook, while we are declarining that hook after that code. Hence, how the code
detect something which are created after that code. Thanks,
tyler
Wed, 07/13/2016 - 10:08
Permalink
After the module count, we're
After the module count, we're not declaring the hook, we're invoking it. These days I recommend using this one line approach instead:
This would invoke all implementations of hook_entity_delete_message_alter, and is much cleaner and consistent with Drupal.