What is a HOOK/hook Tutorial

Fr., 20/07/2012 - 15:47
Body
Hooks allow modules to interact with the Drupal core. Hooks allow modules to add their own stuff to Drupal´s workflow. 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 call that hook in all enabled modules that implement it. Naming convention: The available hooks to implement are explained here in the Hooks section of the developer documentation : http://api.drupal.org/api/group/hooks/6. Each hook has a defined set of parameters and a defined returned type. The string ?hook? is used as a placeholder for the module name is 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(). How hooks work? : Drupal offers a system of callback hooks at various parts of the core code. The hooks are placed in key locations within the core code, such as when adding a user,publishing a blog etc? When the core code hits a hook, it will call out to all implementers of a given hook, finding implementers by looking for method names that adhere to the naming scheme. For example hook_perm function is used to setup new permission types that can be granted to user account roles. If you go to the permissions page at drupal/?q=admin/user/permissions you will see a list of permissions. permission Drupal checks for all the modules that implements the ?hook_perm? In this case blog and block module implement the hook_perm as blog_perm and block_perm respectively. Similarly all implementations of hook_perm are called to generate the final list. Thus modules are adding permissions to the list by using hook_perm. Some modules like blogapi do not implement hook_perm, that?s why it is not present in the list. So during the step by step process of executing a request Drupal executes hooks at certain points. While executing hooks Drupal finds functions in modules which implement that particular hook and executes those functions as well. Thus this way modules can intervene and add to the normal workflow of Drupal.

Creating own Hooks

Hooks you need http://api.drupal.org/api/drupal/includes--module.inc/function/module_invoke/8 http://api.drupal.org/api/drupal/includes--module.inc/function/module_invoke_all/6 http://api.drupal.org/api/drupal/includes--module.inc/function/module_implements/6 http://api.drupal.org/api/drupal/includes--module.inc/function/module_hook/6 Articles http://himerus.com/blog/himerus/creating-hooks-your-drupal-modules http://alanstorm.com/drupal_module_hooks

Basics

Essentially there are 2 things needed to create your own hook(s).
  • Create your own function that implements module_invoke_all('WHAT_YOU_WANT_YOU_HOOK_TO_BE_CALLED');
  • Create your new module & function using YOURMODULE_WHAT_YOU_WANT_YOU_HOOK_TO_BE_CALLED
Drupal
Add new comment
The content of this field is kept private and will not be shown publicly.

Plain text

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <drupal-entity data-*>
  • Web page addresses and email addresses turn into links automatically.
  • Lines and paragraphs break automatically.