What Core Drupal functions can be used to invoke hooks?

678 views Asked by At

This is a followup to another question I asked abou creating your own hooks.

In the answer and comments, two functions were mentioned

module_invoke_all(...)
drupal_alter(...)

The function module_invoke_all appears to be used to invoke a hook for any module that implements it.

The function drupal_alter appears to invoke a hook for any module that implements it and passes around a persistant data structure between all the hook functions.

Digging through the code, I also found

module_invoke(...)

which appears to let you invoke a specific hook in a specific module.

So, my question is really two questions. First, is my understanding of the above items correct. Second, are there any other core Drupal functions that can be used to invoke hooks implemented in a module?

My end goal is a better understanding of how the pieces of Drupal's architecture come together to form Drupal, the application, as most people use it. I'm starting with trying to understand the module system in isolation. Any corrections to glaring misconceptions are appreciated

1

There are 1 answers

3
Berdir On BEST ANSWER

First question: Yes.

Second question: Apart from using these functions, you can also call module_implements() directly and call them manually. Example use case is when you want to pass arguments by reference but don't want an hook_something_alter() naming scheme that drupal_alter() forces you into.

module_implements() returns an array of modules that implemented the given hook. In Drupal 6 this is simply a loop over all modules and then checking if the function $module . '_' . $hook exists.

In Drupal 7, it is for example possible to define that your hook_yourmodule_something can be in a anothermodule.yourmodule.inc and Drupal will then automatically look for that file and include it when necessary. See hook_hook_info. Additionally, it is also possible to alter the list of modules that implement a hook, which is rather crazy and should be used with care. See hook_module_implements_alter.

Because these features make the discovery quite a bit slower than in D6, a cache was added to that. This basically means that whenever you add a hook implementation in D7, you need to clear the cache.

Example manual implementation:

<?php
// Custom hooks should always be prefixed with your module name to avoid naming conflicts.
$hook_name = 'yourmodule_something';
// Get a list of all modules implementing the hook.
foreach (module_implements($hook_name) as $module) {
  // Build the actual function name.
  $function = $module . '_' . $hook_name;
  // Call the function. Anything passed in to the function can be by-reference if the
  // hook_implementation defines it so. If you don't want that, you might want to give them
  // only a copy of the data to prevent abuse.
  $function($arg1, $arg2, $arg3, $arg4);
}
?>

If you look at the code of the functions you linked in your question, you can see that they are basically just helper function for the same procedure, so this should also help to improve the general understanding of hook calling.