1 user.module user_register_submit($form, &$form_state)

Submit handler for the user registration form.

This function is shared by the installation form and the normal registration form, which is why it can't be in the user.pages.inc file.

See also

user_register_form()

File

core/modules/user/user.module, line 3281
Enables the user registration and login system.

Code

function user_register_submit($form, &$form_state) {
  $user_email_verification = config_get('system.core', 'user_email_verification');
  $admin = $form_state['values']['administer_users'];

  if (!$user_email_verification || $admin) {
    $pass = $form_state['values']['pass'];
  }
  else {
    $pass = user_password();
  }
  $notify = !empty($form_state['values']['notify']);

  // Remove unneeded values.
  form_state_values_clean($form_state);

  // Convert checkbox values to an unindexed list.
  if (isset($form_state['values']['roles'])) {
    $form_state['values']['roles'] = array_keys(array_filter($form_state['values']['roles']));
  }

  $form_state['values']['pass'] = $pass;
  $form_state['values']['init'] = $form_state['values']['mail'];

  $account = $form['#user'];

  entity_form_submit_build_entity('user', $account, $form, $form_state);
  $status = $account->save();

  // Terminate if an error occurred while saving the account.
  if ($status != SAVED_NEW) {
    backdrop_set_message(t("Error saving user account."), 'error');
    $form_state['redirect'] = '';
    return;
  }
  $form_state['user'] = $account;
  $form_state['values']['uid'] = $account->uid;

  watchdog('user', 'New user: %name (%email).', array('%name' => $form_state['values']['name'], '%email' => $form_state['values']['mail']), WATCHDOG_NOTICE, l(t('edit'), 'user/' . $account->uid . '/edit'));

  // Add plain text password into user account to generate mail tokens.
  $account->password = $pass;

  // New administrative account without notification.
  $uri = $account->uri();
  if ($admin && !$notify) {
    backdrop_set_message(t('Created a new user account for <a href="@url">%name</a>. No email has been sent.', array('@url' => url($uri['path'], $uri['options']), '%name' => $account->name)));
  }
  // No email verification required; log in user immediately.
  elseif (!$admin && !$user_email_verification && $account->status) {
    _user_mail_notify('register_no_approval_required', $account);
    $form_state['uid'] = $account->uid;
    user_login_submit(array(), $form_state);
    backdrop_set_message(t('Registration successful. You are now logged in.'));
    $form_state['redirect'] = '';
  }
  // No administrator approval required.
  elseif ($account->status || $notify) {
    $op = $notify ? 'register_admin_created' : 'register_no_approval_required';
    _user_mail_notify($op, $account);
    if ($notify) {
      backdrop_set_message(t('A welcome message with further instructions has been emailed to the new user <a href="@url">%name</a>.', array('@url' => url($uri['path'], $uri['options']), '%name' => $account->name)));
    }
    else {
      backdrop_set_message(t('A welcome message with further instructions has been sent to your email address.'));
      $form_state['redirect'] = '';
    }
  }
  // Administrator approval required.
  else {
    _user_mail_notify('register_pending_approval', $account);
    backdrop_set_message(t('Thank you for applying for an account. Your account is currently pending approval by the site administrator.<br />In the meantime, a welcome message with further instructions has been sent to your email address.'));
    $form_state['redirect'] = '';
  }
}