3

I understand how to use hooks (mymodule_HOOKNAME()) but what are functions used for that actually start with hook? Ex: hook_action_info()

When I am creating custom hooks I normally just use: module_invoke('MODULE', 'HOOK_NAME').

1
  • module_invoke_all() can be used as well. depending on how you wish to use it. Commented Aug 1, 2011 at 12:51

3 Answers 3

3

hook_action_info and other functions that begin with hook_ exist only to document the contract between the caller of the hook and the module that implements it.

1
  • Correct answer. Just would like to add that if others will use your code you should write your own hook_ functions to help them understand yours. Commented Aug 1, 2011 at 12:48
1

First off, there's not a single function in Drupal, that starts with hook_.

hook_

Whenever you see a function name starting with hook_, it is a symbolic name for a hook function to be implemented by a module or a theme, where hook_ will be replaced with the module's or theme's actual name.

Within your module, all functions you want to expose to the Drupal's hook system to be called at appropriate times, will start with mymodule_ as long as the name of your module is actually mymodule. That is also the MODULE part in

module_invoke('MODULE', 'HOOK_NAME')

mymodule_HOOKNAME

On the other hand, mymodule_HOOKNAME() refers to all or any hook functions implemented by module mymodule.

All modules that wish to configure custom actions will have to implement a function which starts with the module name and ends in _action_info. Again, that's the HOOK_NAME part in

module_invoke('MODULE', 'HOOK_NAME')

module_invoke

Finally, the module_invoke('MODULE', 'HOOK_NAME') just puts these two concepts (module name and hook name) together to build a specific module's hook function name, check if it exists at all and then call it.

To work out the function name to call, module_invoke puts together the MODULE and HOOK_NAME parts, separated by an underscore. And that is why yourmodule implementing hook_action_info() defines a function yourmodule_action_info().

0
1

From the Drupal 7 API:

Allow modules to interact with the Drupal core.

Drupal's module system is based on the concept of "hooks". A hook is a PHP function that is named foo_bar(), where "foo" is the name of the module (whose filename is thus foo.module) and "bar" is the name of the hook. Each hook has a defined set of parameters and a specified result type.

To extend Drupal, a module need simply implement a hook. When Drupal wishes to allow intervention from modules, it determines which modules implement a hook and calls that hook in all enabled modules that implement it.

The available hooks to implement are explained here in the Hooks section of the developer documentation. The string "hook" is used as a placeholder for the module name in the hook definitions. For example, if the module file is called example.module, then hook_help() as implemented by that module would be defined as example_help().

The example functions included are not part of the Drupal core, they are just models that you can modify. Only the hooks implemented within modules are executed when running Drupal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.