You are here

function email_registration_user_insert in Email Registration 7

Same name and namespace in other branches
  1. 8 email_registration.module \email_registration_user_insert()

Implements hook_user_insert().

File

./email_registration.module, line 11
Allows users to register with an e-mail address as their username.

Code

function email_registration_user_insert(&$edit, &$account, $category = NULL) {

  // Don't create a new username if one is already set.
  if (!empty($account->name) && strpos($account->name, 'email_registration_') !== 0) {
    return;
  }

  // Other modules may implement hook_email_registration_name($edit, $account)
  // to generate a username (return a string to be used as the username, NULL
  // to have email_registration generate it).
  $names = module_invoke_all('email_registration_name', $edit, $account);

  // Remove any empty entries.
  $names = array_filter($names);
  if (empty($names)) {

    // Strip off everything after the @ sign.
    $new_name = preg_replace('/@.*$/', '', $edit['mail']);

    // Clean up the username.
    $new_name = email_registration_cleanup_username($new_name, $account->uid);
  }
  else {

    // One would expect a single implementation of the hook, but if there
    // are multiples out there use the last one.
    $new_name = array_pop($names);
  }

  // Ensure whatever name we have is unique.
  $new_name = email_registration_unique_username($new_name, $account->uid);

  // Replace with generated username.
  db_update('users')
    ->fields(array(
    'name' => $new_name,
  ))
    ->condition('uid', $account->uid)
    ->execute();
  $edit['name'] = $new_name;
  $account->name = $new_name;
}