You are here

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:

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'].

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.

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"

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:

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!

In the following code, I did not understant oen thing. How module_implements() function will determine how many modulesimplemets the 'chnage_my_entity_delete_message' hook, while we are declarining that hook after that code. Hence, how the codedetect something which are created after that code. Thanks,

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