Is it possible to create your own hook in a Drupal module for other Drupal modules to consume? If not, is there a mechanism in Drupal for third party developers to provide hooks? If everything's been a no so far, where in the core are the list of hooks implemented?
As I understand things, Drupal modules work on a event like system called hooks. When you create a new module, you create functions that implement a hook. For example, there's a hook_delete
hook. If you implement a function in your module
function mymodule_delete($node) { }
this function will be called whenever a node is deleted.
What I want to know is, is there a way or me, as a third party module developer, to create my own hooks. Say, something like hook_alanskickbutthook
so that other module developers could subscribe to this hook.
If this is possible, how do you do it? I've looked around the official docs and haven't found much there, and I still get a little dizzy when I start poking around the Drupal source code (I understand recursion, but don't spend enough time thinking about recursive problems). Full solutions are welcome, but I'm happy to just be pointed in the right direction.
In Drupal 7 and earlier versions hook_menu has been the Swiss Army knife of hooks. It does a little bit of everything: page paths, menu callbacks, tabs and local tasks, contextual links, access control, arguments and parameters, form callbacks, and on top of all that it even sets up menu items!
A hook is a way to place a piece of your own custom code to be run by Drupal. Using hooks, you can ask Drupal to run a piece of code when a node is viewed/edited/deleted.
The Drupal 6. x menu system introduces two new _alter hooks for changing the items being saved to the {menu_router} or {menu_links} tables.
Module_invoke_all() is your ticket to creating your own hooks:
see the API:
http://api.drupal.org/api/drupal/includes--module.inc/function/module_invoke_all
and then look at this great writeup:
http://web.archive.org/web/20101227170201/http://himerus.com/blog/himerus/creating-hooks-your-drupal-modules
(edit: was at http://himerus.com/blog/himerus/creating-hooks-your-drupal-modules but this is now gone)
Once you've made your hook, it can be called in another module using:
/** * Implementation of hook_myhookname() */ function THISMODULENAME_myhookname(args){ //do stuff }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With