Skip to content

Instantly share code, notes, and snippets.

@mandelbro
Created June 18, 2012 01:20
Show Gist options
  • Save mandelbro/2946288 to your computer and use it in GitHub Desktop.
Save mandelbro/2946288 to your computer and use it in GitHub Desktop.
Enable login with user id or email in Drupal 6
/**
* Implements hook_form_alter
*/
function trnAccount_form_alter(&$form, &$form_state, $form_id) {
global $user;
switch($form_id) :
case 'user_login':
// build the validators array
$validators = array('trnAccount_login_validate') + user_login_default_validators();
// override default validators
$form['submit']['#validate'] = $validators;
break;
}
}
/*
* uberacchount_login_validate
* Created by: mandelbro
* Login validation handler which enables login with either id or email
*/
function trnAccount_login_validate($form, &$form_state) {
global $user;
// get the mail from from_state values
$mail = $form_state['values']['name'];
// check if the name field is an email address
if(!valid_email_address($mail)) return; // no need to move forward
// query the user table for the email
if($name = trnToolkit_get_var("SELECT name FROM {users} WHERE mail = '%s'", array($mail))) {
// if we found a username, swap it for the email in form_state values
$form_state['values']['name'] = $name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment