4

I want to create a hook function so that other modules implement it, any pointers to how it is done?

3

You don't create a hook, in the same way Drupal core doesn't create hook_nodeapi(), or hook_hook_info(). You write code that uses (invokes) the implementation of a hook done in other modules. The modules that want to interact/integrate with yours will implement that hook, and your module will use module_invoke(), module_invoke_all(), or drupal_alter() to invoke those hooks.

The correct phrase is not creating a hook, but defining a hook, which is essentially documenting that your module invokes a specific hook.

  • Re-read the original question: "I want to create a hook function so that other modules implement it..." The question asks how to create a hook that other modules can over-ride - the opposite of your statement. – DeeZone Sep 20 '11 at 10:04
  • I read the question, and my answer doesn't change. You don't create a hook that other modules use; you invoke the hook implementation done by other modules. – kiamlaluno Sep 20 '11 at 11:17
  • 2
    I think you might be getting hung up on "semantics" but I understand your point. Perhaps most importantly the coding solution to Firdous' question is both in my answer below and your edited response above. It's all good, other readers will now have a solution either way. – DeeZone Sep 21 '11 at 3:24
  • 2
    Drupal 7 introduced the yourmodule.api.php file to document a hook that your module defines, that is the only thing that you need to (should) create. It is strongly suggested to follow that standard as it the first place where Drupal developers can look for the hooks a module defines and their documentation. Regarding naming semantics, the opposite would be to implement a hook. – Berdir Oct 5 '11 at 3:09
7

Essentially there are 2 things needed to create your own hook(s).

  • Create your own function that calls module_invoke_all('WHAT_YOU_WANT_YOUR_HOOK_TO_BE_CALLED');
  • Create your new module and a function called YOURMODULE_WHAT_YOU_WANT_YOUR_HOOK_TO_BE_CALLED()

That is ALL it actually takes to implement your own hooks in a Drupal module. The great thing here is the flexibility that it provides for you to do anything, AND allow other developers to easily extend your work. The major consideration I can see here is naming your hook. You should obviously try to choose something unique to your module, that a core or another contributed module wont be using.

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.