5

I've installed a contrib module in Drupal 7 and would like to hook into it. However, the documentation for the contrib module is sparse and doesn't list the available hooks, though it implies that there are some.

Is there a standard "Drupal way" of seeing what hooks a contrib module exposes?

(I've deliberately not mentioned which module this is as I'd like a generic answer if one exists.)

8

By convention a lot of modules include a modulename.api.php file in the root folder, which describes the hooks implemented.

They may also implement hook_hook_info() to programatically describe the hooks to Drupal.

If that file isn't there, have a look at the other documentation/non-code files included. Sometimes you'll find examples elsewhere in the folder structure.

There's nothing enforced, though, so there isn't a generic all-encompassing answer. For a low tech solution you could grep the module folders for patterns like

  • module_invoke('modulename
  • module_invoke_all('modulename
  • module_implements('modulename

If you use the grep method, remember to add a conditional for the quote before the module name, it could be single or double.

In case the above causes confusion:

By convention most hooks are prefixed with the module's name, so that method will work 99% of the time, and avoid false-positives when a module invokes hooks for anything other than itself.

There will be the odd instance when it doesn't, but since there's no formally enforced way for a module to define hooks, anything non-manual here is going to be a best effort however you look at it.

  • I also found a method in the contrib module called hook_<contrib-module-name> which seemed to handle all possible hooks. I don't know whether that's a common convention too? – Sam May 23 '13 at 8:18
  • Not one that I've seen before no...what module is it out of interest? – Clive May 23 '13 at 8:22
  • It's gmap – Sam May 23 '13 at 8:23
  • Ah yeah I see what you mean. That's really a hangover from Drupal 6 thinking (e.g. the old hook_nodeapi() with a bunch of different operations). In Drupal 7 (and going forward) the onus is on separating those operations out into individual hooks (e.g hook_node_insert(), hook_node_update(), hook_node_delete()). So yeah you probably will see that convention used here and there, but increasingly less so I'd imagine – Clive May 23 '13 at 8:35
3

The only way to know which hooks are used from a module is looking for any of the following function calls.

Some modules have their own function that calls one of those functions. For example, the User module uses user_module_invoke(), and the Node module uses node_invoke().

  • That's not the only way though...what about the API.php file and hook_hook_info? – Clive May 22 '13 at 16:27
  • Not all the modules implement hook_hook_info() (see user.module), and in some cases a module implements it for hooks implemented also by other modules (see system.module). Modules should have a file describing the hooks it uses, but that is not always the case; there is also the case that file has not been updated. Looking at the module code, you can be sure not to miss any hook. – kiamlaluno May 22 '13 at 17:10
  • Totally agree (I've alluded to the same thing in my answer), as far as 'foolproof' goes manually looking through the code is as good as you can get really – Clive May 22 '13 at 17:13
  • Thanks! Your explanation of which argument was the hook name in each case was really helpful. – Sam May 23 '13 at 8:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.