Custom Validate and Submit function for Login, Register & Forgot password modules in Drupal 7 ?

By

April 25, 2013Code DrupalNo comments

Why and Where we need custom override ?

Drupal 7 has its own core functionality for Login, Register & Forgot-Password module. In some project cases there will be need of changing the functionality of these core modules. In that case we should not over write the core modules to achieve the functionality instead we should override the functionality with our custom module.

How to custom Validate & Submit functionality for core modules?

To override the functionality of core modules in your custom module you should be aware of few Drupal hooks. Let’s see what are the essential hook you need to know.

  1. hook_form_FORM_ID_alter() 
  2. hook_form_alter()

Where you should use hook_form_FORM_ID_alter() ?

In your application if you want to alter only one specific form then in that case you can use this hook.

for eg:- If you want to alter Forgot Password form in your module then you should write a hook like MODULE_NAME_form_user_pass_alter()

login_drupal

Where you should use hook_form_alter()?

Suppose if you have an application which should alter multiple core functionality forms then you should go for hook_form_alter().

Both these hooks will contain three (&$form, &$form_state, $form_id) parameters in it. The last parameter will have the form_id for all forms. So in your custom module you can write Switch case with form_id and achieve the functionality.

for eg:- You can write this hook in your module like MODULE_NAME_form_alter()

Implementation of  overriding core Validate and Submit functionality,

In custom_user_pass_validate_function and custom_user_pass_submit_validate_function you can override the user_pass validate and submit functionality. This example is based on User forgot password module likewise you can implement the same for user_login & user_register_form modules.

You can write your custom code for validation and submition with your custom functions like,

These are all about writing custom validate and submit functionality for core modules in D7.

Leave a Reply