I've created 2 files:
login_process.infologin_process.module
I've created the table "user" in the database that Drupal is using. I want to display a custom login form with fields "Username" and "Password".
Assuming the data from Username and Password fields are in the correct format(proper validations), when the submit button is pressed, a query is done and data to check if the values matches that found in the table.
If no result is found, it displays an error message else it redirects to to the welcome page. Here are the codes:
login_process.info
name = "My login process module" description = "Performs login process" core = "7.x" version ="7.x.1.0" package = "Login/Register"` login_process.module
<?php /* *Implements hook permission */ function login_process_hook_permission() { return array( 'submit login_process' => array( 'title' => t('Submit login form'), 'description' => t('Submit the login form') ), ); } /* * Implements hook menu */ function login_process_hook_menu() { $items = array(); $items['login-process'] = array( 'title' => 'login', 'type' => 'MENU_NORMAL_ITEM', 'access arguments' => array('submit login_process'), 'page callback' => 'drupal_get_form', 'page arguments' => array('login_process_form') ); return $items; } /* * The login form */ function login_process_form($form,&$form_state) { $form['Username'] = array( '#type' => 'textfield', '#title' => t('Username'), '#size' => 20, '#maxlength' => 20, '#required' => TRUE, '#description' => t('Please enter a valid username'), ); $form['Password'] = array( '#type' => 'textfield', '#title' => t('Password'), '#size' => 20, '#maxlength' => 20, '#required' => TRUE, '#description' => t('Please enter a valid password'), ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Add item') ); return $form; } /* * Validation handler for the login_process_form */ function login_process_form_validate($form,&$form_state) { print('<pre>'.print_r($form_state['values'],1).'<pre>'); die(); if (!preg_match("/[a-zA-Z]/",$form_state['values']['Username'])) { form_set_error('Username', t('Username must contain only alphabets')); return FALSE; } return TRUE; //add more validations for Username and Password } /* *Submit handler for the login form */ function login_process_form_submit($form,&$form_states) { $username = $form_state['values']['Username']; $password = $form_state['values']['Password']; $results = db_query("SELECT Username,Password FROM user WHERE Username='".$username."' AND Password='".$password."'")); if(is_null($results)) { form_set_error('Username', t('Wrong username or password. Try again!!!!')); return FALSE; } else { echo "Welcome".$form_state['values']['Username']."<a href="logout.php">Logout</a>" // it will be better if it is displayed in a block } } It neither display errors nor the login form created from the module. Can anyone help me here? Do I have to install additional modules to make it work?
Most of the codes are based from Daily dose of Drupal episode 16 to 22.
modules/user/user.modulefile, starting from thehook_menu()implementation and the wayuser/loginpath is defined, and then working your way out to other parts.