Quantcast
Viewing all articles
Browse latest Browse all 3

Module: Login With Email or Username Drupal

I don’t know why drupal doesn’t have this built in… but this will allow you to have users login with their email address or their username. People can usually remember their email address better than the username that they used on some random site. Doubly so if their username was not their usual one because someone keeps taking their normal one (I’m not bitter…).

This is the code as of version 1:

<?php
/**
 * 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 email_login_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    //there are two normal login forms
    case 'user_login':
    case 'user_login_block':
      // 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');
      break;
    //ignore other stuff
    default:
      return;
      break;
  }
}
 
/**
 * 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']) && strpos($form_state['values']['name'], '@') !== false) {
    if(module_exists('multiple_email')){
        $name = db_result(db_query("SELECT users.name FROM {users} JOIN multiple_email ON multiple_email.uid = users.uid WHERE (LOWER(multiple_email.email) = LOWER('%s') AND multiple_email.confirmed = TRUE) OR LOWER(users.mail) = LOWER('%s')", $form_state['values']['name'], $form_state['values']['name']));
    } else {
        $name = db_result(db_query("SELECT name FROM {users} WHERE LOWER(mail) = LOWER('%s')", $form_state['values']['name']));
    }
    if ($name) {
      form_set_value($form['name'], $name, $form_state);
    }
  }
}

I have made a slight change to the code from that of login toboggan, I added the check to see if it at least looked like an email address (has an @ symbol in it) before doing the extra database call. It probably doesn’t make too much of a difference in the grand scheme of things though. Another thing that it does is when Multiple E-mail Addresses is active, it checks against the multiple email addresses table and matches it up to the user account.

Get the module here: Drupal Email Login.

Image may be NSFW.
Clik here to view.
flattr this!


Viewing all articles
Browse latest Browse all 3

Trending Articles