blog-banner

Drupal - Email Only Login

  • EMAIL ONLY
  • REGISTER

You will sometimes want to restrict users to register/login in using only their email addresses, not username. If you simply want to let login using both, then you could check logintoboggan or email registration.

I assume you will have some first-name fields in the registration form.

Alter registration form:

function HOOK_form_alter(&$form, &$form_state, $form_id) {
  if ("user_register_form" == $form_id) {
    $form['account']['name']['#access'] =  FALSE; //hide username field
    $form['account']['name']['#required'] =  FALSE;
    $form['#validate'][] = 'HOOK_user_register_validate';
  }
}
function HOOK_user_register_validate($form, &$form_state) {
  $form_state['values']['name'] = isset($form_state['values']['mail']) ? $form_state['values']['mail'] : '' ;
}
Sitewide username tweak:
 
The above code will let you save the email address in the username field. Then you have to make Drupal render the first name instead of username (which is the email address now) wherever appropriate. For that use hook_username_alter(),
 
function HOOK_username_alter(&$name, $account) {
  //load the full user object, since $account not always provide all informations
  $user = user_load($account->uid);
  if (!empty($user->field_first_name)) {
    //get firstname value, mutilanguage support
    $firstname = field_get_items('user', $user, 'field_first_name');
    if ($firstname) {
      $name = $firstname[0]['value'];
    }
  }
}
 
Now your site will show the first name in all places instead of the email address and users will never know about the username's existence.
 
Cheers :)

 

Get awesome tech content in your inbox