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.
function mymodule_form_alter(&$form, &$form_state, $form_id)
'#title'=>t('Mobile Number'), //Changes the tile of name field
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.
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.
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.
file : mymodule.module
// 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