1

I'm a newbie in Drupal and I have an issue with the User module (I think).

In my user registration form there is no username field but there is email field provided by the Email Registration module. When users register, Drupal generates a username from given email using its first part before @ and appends user ID to it. For example: if I will try to register with email [email protected], Drupal will generate username for my account user_123 where 123 - is user ID.

How can I change this default behavior and prevent Drupal from adding _123 to generated username? Is there is any solution to get rid of the username, and use the email?

2
  • In vanilla Drupal there is no way to hide the username field upon registration and it does not by default append the uid to the username... So I feel you have forgotten to mention some details in your question. Commented Oct 18, 2016 at 16:31
  • Yes, you're right! I forgot that I'm using module Email Registration for this purpose.
    – Roman
    Commented Oct 18, 2016 at 17:13

1 Answer 1

3

While looking at the project page of Email Registration, it reads:

A username is generated and assigned based on the user name part of the email address and their user ID. Sites that want to create usernames in their own way can do so by implementing hook_email_registration_name.

And when looking at hook_email_registration_name(), we see how we can alter the username by returning a value.

So, what you need to do is:

  • Create a custom module (there is plenty of documentation for that on this site and the rest of the internet).
  • Implement hook_email_registration_name() (place this function in your module file and replace hook with your module name).
  • Have the hook return a new username.

So you need to put something like this in your module file:

function mymodule_email_registration_name($edit, $account) {
  $mail = $edit['mail'];
  $mail_parts = explode('@', $mail);
  $username = $mail_parts[0];
  return email_registration_cleanup_username($username);
}

This will return the first part of the email as the username. So [email protected] becomes john. [email protected] becomes bob. [email protected] becomes also john... But that was already in use and Drupal does not allow duplicate usernames, so you have a problem. (And you discovered the reason this module by default appends a unique token to the username.)

You could solve this by implementing a small iterator. Appending 1, 2, 3 to every subsequent John but it would result in just as ugly usernames (and for the 100th john you had to do 99 database lookups making this slow).

So I'd like to propose to you Real Name. This nifty module will override the username display everywhere with the value of one or more other profile field. So you add a namefield to the registration, configure Real Name to use that field and the user will never know his username is john_123 because he will never see it.

I don't know your specific requirements and this might not be suitable for you, or if you can think of another way to enforce uniqueness, you can always use the hook and implement your own method. See what fits you :).

2
  • Thank you mr. Neograph734 for this excellent answer! It helped me a lot!
    – Roman
    Commented Oct 18, 2016 at 18:32
  • Great! Good luck :) Commented Oct 18, 2016 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.