Quantcast
Channel: SG, Author at Mydons
Viewing all articles
Browse latest Browse all 41

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

$
0
0

drupal_mydons

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,

/*
 * Implementation of hook_form_FORM_ID_alter().
 */

function MODULE_NAME_form_user_pass_alter(&$form, &$form_state, $form_id) {
    $array_key = array_search('user_pass_validate', $form['#validate']);

    if ($array_key === FALSE)
        array_unshift($form['#validate'], 'user_pass_validate');
    else
        $form['#validate'][$array_key] = 'custom_user_pass_validate_function';

    $array_key_sub = array_search('user_pass_submit', $form['#submit']);

    if ($array_key_sub === FALSE)
        array_unshift($form['#submit'], 'user_pass_submit');
    else
        $form['#submit'][$array_key_sub] = 'custom_user_pass_submit_validate_function';

    return $form;
}
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,
/*
 * Custom Validate function.
 */

function custom_user_pass_validate_function($form, &$form_state) {
    // validate the form here
}

/*
 * Custom Submit function.
 */

function custom_user_pass_submit_validate_function($form, &$form_state) {
 // custom database connection or some logic 
}
These are all about writing custom validate and submit functionality for core modules in D7.

The post Custom Validate and Submit function for Login, Register & Forgot password modules in Drupal 7 ? appeared first on Mydons.


Viewing all articles
Browse latest Browse all 41

Trending Articles