17

How can I create my own hook (similar to the ones Drupal core creates) for other Drupal modules to utilize?

20

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.

  • 6
    +1 about the documentation, that is a new coding standard from Drupal 7 and many contrib modules started using it for D6 too. Note that instead of an empty function, it's even better when you provide an example implementation to show what can be done with that hook. – Berdir Mar 7 '11 at 21:13
  • 1
    Usually, you provide code for the hook listed in MODULE.api.php if there is an actual implementation of that hook. – kiamlaluno Mar 8 '11 at 15:42
0

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.

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.