Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you Create your Own Hook in Drupal?

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.

like image 308
Alan Storm Avatar asked Feb 14 '11 16:02

Alan Storm


People also ask

What is hook menu in Drupal?

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!

What are hooks in Drupal 7?

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.

Which hook is used to alter Drupal?

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.


1 Answers

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 } 
like image 124
jpstrikesback Avatar answered Sep 22 '22 12:09

jpstrikesback