Skip to main content
login with email

Drupal - email only login

You will sometime want to restrict user to register/login 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 field in 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 username field. Then you have to make Drupal render the firstname instead of username (which is 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 email address and user will never know about the username existance. 
 
Cheers :)

 

Comments

kanzer (not verified)

Wed, 10/01/2014 - 15:39

I have logintobogan where I set that in the registration form is not required username

Where do I can put the code above?

In my template the theme?

Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.