Question
How can I create my own hook (similar to the ones Drupal core creates) for other Drupal modules to utilize?
Solution
You don't actually create hooks. You use helper functions such as module_invoke_all()
, module_invoke()
or drupal_alter()
to invoke all functions matching the expected name pattern. The invoked functions are usually found with module_implements()
.
Even if it is not needed for the hooks to work, the best practice is to document them in MODULE.api.php
by creating empty stubs hook_NAME
functions with documentation comments.
OTHER TIPS
You can also create hook, using hook_trigger_info you can create new hook, and in your module you need to implement it. As an example, suppose you want to create an archive of nodes and put old nodes in it. You also want to trigger a hook when the archive operation is done. it is going to be some thing like this
function hook_trigger_info() {
return array(
'node' => array(
'archive_nodes' => array(
'label' => t('Archive old nodes'),
)
)
);
}
After defining the hook, your module is responsible for implementing the hook and actions using module_invoke() or module_invoke_all() .
You can enable the trigger
module to see this new hook under the node tab.