Introduction to Drupal Hook System Part-1

Drupal hooks allows module to interact with drupal core system. Drupal module system is based on “hooks”. A hook is nothing just PHP user defined functions that is named foo_bar($vars). where “foo” is name of the module name “bar” is the name o

f hook.
e.g. if your module name is “mymodule” then in “mymodule.module” you will create hook_menu().

1
2
3
4
5
6
7
8
<?php
/**
*Implementation of hook_menu
**/
function mymodule_menu()
{
    // do your code to generate menu item.
}

If you want to integrate your custom drupal module with drupal core then you will have to use drupal hooks. Some important hooks which make your work easier in custom module development are as follows :

Hook_init()

This drupal hooks does not require any parameters. this hook fires when website loads. It generally used when you have to add javascript or css for custom module.
For more information about hook_init() click here

Hook_menu()

Like hook_init() this hook also does not require any parameters. It is mainly used to add custom menu item in any Menus e.g. in primary links, navigation links and secondary links etc.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
/**
*Implementation of hook_menu
**/
function mymodule_menu()
{
    $item['admin/settings/my_menu']=array(
        'title' => t('My Menu Title'),        //title of menu
        'description' => t('My Menu Description'),    //description of menu
        'page callback' => 'my_menu_func', //function fired when one click on menu
        'access arguments' => array('administer site configuration'),  //access arguments
    );
    return $item;    //return item array in end
}

For more information about hook_menu() visit site

hook_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL)

This is a very important hook of drupal. It fires every time when any action performed on node like view node, delete, print, edit, update, insert etc.
it requires 4 parameters. where last two are optional.
Parameter 1 (&$node) contains all the information of node. on which action is taken.
Parameter 2 ($op) contains the current action weather node is being viewed or delete or update etc.
Usage example :
file: mymodule.module

1
2
3
4
5
6
7
function mymodule_nodeapi(&#038;$node, $op, $a3 = NULL, $a4 = NULL)
{
    if($op=='insert' &#038;& $node->type=='page' &#038;& $node->isnew==1)
    {
       //do something on insertion of new page
    }
}

For more information about hook_nodeapi() click here

hook_user($op, &$edit, &$account, $category = NULL)

This hooks allows module to act when any operation are performed on drupal user account. e.g. If you want to update any extra information when some user upates his data or admin updates some user then in that case you will have to user hook_user in your module. This hook requires 4 parameters where last one is optional.

Parameter1 ($op) : contains that what kind of operation is being performed on user account like update, delete , insert etc.
parameter2($edit) : It is array of User information submitted by user.
parameter3 ($account) : It is object containing old information of user or current information of user.
Usage example :

file: mymodule.module

1
2
3
4
5
6
7
function mymodule_user($op, &#038;$edit, &#038;$account, $category = NULL)
{
   if($op=='insert' || $op=='after_update')
   {
      //do something on insertion or update of users in drupal
   }
}

For more information about this hook click here

Note : Some of Hooks are no longer available in drupal 7. these hooks available in drupal 4 to drupal 6.

Well this is all about some of the important drupal_hooks. Will come back with some of more important posts on drupal.

Comments
  1. Sombir Jangra
    • admin
      • Bettie
  2. Lai Ponsler
    • Ghan
      • Kailyn
  3. Ryan Johnson
  4. Sac Hermes
  5. Broom
  6. Eel
  7. Alsdon
    • Lane Boisseau
  8. Casque Dre
  9. Sac Hermes
  10. Elbert Shelvin
  11. Henkey
  12. kuldeep

Leave a Reply

Your email address will not be published. Required fields are marked *


*