Drupal Hook System Part-II

In the previous article we have discussed about drupal hook System and how we use drupal hooks in the module developmen

t. Now in this article we will discuss some other drupal hooks .

  • Hook_form_alter(&$form, &$form_state, $form_id)
    This hook is used to alter the form before form is rendered or displayed. e.g. If we want to alter the login form of drupal then we will use hook_form alter.
    Parameter 1( &$form)= Array of form element which is being rendered.
    parameter2(&$form_state)= Contains the default values or field value in the form.
    Parameter3($form_id)=Contains the unique ID of form.
    The following code shows how to change the text of “username” field to “Mobile no” in login form.
    file: mymodule.module 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    function mymodule_form_alter(&$form, &$form_state, $form_id)
    {
      if($form_id=='user-login')
     {
        $form['name']=array(
          '#title'=>t('Mobile Number'), //Changes the tile of name field
          '#type'=>'textfield',  //
          '#required'=>true,
       );
       return $form;
      }
    }

    So in the above example we have altered the title of a field whose name is “name” (username field in user login form) and replace it with “Mobile Number”.
    Note : hook_form_alter work on all the forms so we have to check the form by form id. you can also use hook_form_FORM_ID_alter() where FORM_ID is ID of form.

  • hook_cron()
    This hook does some task periodically. if you want to enable your module to schedule some task at regular intervals then in that case you will use hook_cron().
    This function doesn’t require any parameters.
    Usage :
    file mymodule.module 

    1
    2
    3
    4
    5
    
    function mymodule_cron()
    {
       drupal_set_message("Scheduled task run") // set the message
       // do something extra...
    }

    In the above example it will shows the message “schedule task run ” at regular intervals of time. You can check it by run http://www.example.com/cron.php . When you execute this file and go to any page, the message ‘Scheduled task run’ will display.

  • hook_comment(&$a1, $op)
    As the module name describes that this hook works on comment system. for example if you want to do some action on submit or delete or edit comment . then you will have to use hook_comment.
    parameters(&$a1)= contains the array of current comment which is being edited or deleted or inserted.
    Parameter ($op)= Current action which is taking place like insert,delete, update.
    usage
    file : mymodule.module 

    1
    2
    3
    4
    5
    6
    7
    
    function mymodule_comment(&$a1,$op)
    {
         if($op=='insert')
         {
             // do some task when comment is being inserted in database
         }
    }

    Note: When you publish, unpublish or edit the comment from comment edit page the $a1 comes in the form of an array, but when you approve the comment from admin panel then $a1 comes in the form of an object.

Now thats all for this article. In the next article we’ll discuss about difference between Druapal 6 and Drupal 7

Comments
  1. Seanger
  2. Nolte
  3. Elmer Cessna
  4. C.Wonka
    • Sam

Leave a Reply

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


*