You are here

function email_registration_user in Email Registration 6

Same name and namespace in other branches
  1. 5 email_registration.module \email_registration_user()

Implementation of hook_user().

File

./email_registration.module, line 13
For registration process without a username

Code

function email_registration_user($op, &$edit, &$account, $category = NULL) {
  switch ($op) {
    case 'insert':

      // 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']);
      }
      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_query("UPDATE {users} SET name = '%s' WHERE uid = '%s'", $new_name, $account->uid);
      $edit['name'] = $new_name;
      $account->name = $new_name;
      break;
  }
}