login_with_email

Categories

Component ID

2777667

Component name

login_with_email

Component type

module

Maintenance status

Development status

Component security advisory coverage

not-covered

Component created

Component changed

Component body

/**
 * Capture the form stubmit and check the form_id if it matches a login form, then
 * it will check it as an email using an extra validation step
 * @param array $form The current form  (I guess...)
 * @param array $form_state The state of the current form  (I guess...)
 * @param string $form_id The unique identifier for this form
 * @see hook_form_alter
 */
function login_with_email_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    //there are two normal login forms
    case 'user_login':
    case 'user_login_block':
      _email_login_user_login_form_alter($form, $form_state, $form_id);
      
      break;
    //ignore other stuff
    default:
      return;
      break;
  }
}

function _email_login_user_login_form_alter(&$form, $form_state, $form_id){
  // Ensure a valid validate array.
  $form['#validate'] = is_array($form['#validate']) ? $form['#validate'] : array();
  // your validation function must run first.
  array_unshift($form['#validate'], 'email_login_user_login_validate');
}

/**
 * Capture the username entered, if there is an @ symbol, it will check it against email addresses instead of the username
 * if multiple_email is installed, check against that table instead
 * @param array $form The current form  (I guess...)
 * @param array $form_state The state of the form (I guess...)
 */
function email_login_user_login_validate($form, &$form_state) {
if (isset($form_state['values']['name']) && $form_state['values']['name']) {
    if ($name = db_query("SELECT name FROM {users} WHERE LOWER(mail) = LOWER(:name)", array(
      ':name' => $form_state['values']['name'],
    ))->fetchField()) {
      form_set_value($form['name'], $name, $form_state);
    }
  }
}