Prevent the email registration module from appending the user ID as a suffix on new user's username 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 user@mysite.com, 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?
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 john@gmail.com becomes john. bob@yahoo.com becomes bob. john@hotmail.com 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 :).