public function RegisterFormController::save

Overrides Drupal\Core\Entity\EntityFormController::submit().

Overrides EntityFormController::save

File

drupal/core/modules/user/lib/Drupal/user/RegisterFormController.php, line 96
Definition of Drupal\user\RegisterFormController.

Class

RegisterFormController
Form controller for the user register forms.

Namespace

Drupal\user

Code

public function save(array $form, array &$form_state) {
  $account = $this
    ->getEntity($form_state);
  $pass = $account->pass;
  $admin = $form_state['values']['administer_users'];
  $notify = !empty($form_state['values']['notify']);

  // Save has no return value so this cannot be tested.
  // Assume save has gone through correctly.
  $account
    ->save();
  $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) {
    drupal_set_message(t('Created a new user account for <a href="@url">%name</a>. No e-mail has been sent.', array(
      '@url' => url($uri['path'], $uri['options']),
      '%name' => $account->name,
    )));
  }
  elseif (!$admin && !config('user.settings')
    ->get('verify_mail') && $account->status) {
    _user_mail_notify('register_no_approval_required', $account);
    $form_state['uid'] = $account->uid;
    user_login_form_submit(array(), $form_state);
    drupal_set_message(t('Registration successful. You are now logged in.'));
    $form_state['redirect'] = '';
  }
  elseif ($account->status || $notify) {
    if (empty($account->mail) && $notify) {
      drupal_set_message(t('The new user <a href="@url">%name</a> was created without an email address, so no welcome message was sent.', array(
        '@url' => url($uri['path'], $uri['options']),
        '%name' => $account->name,
      )));
    }
    else {
      $op = $notify ? 'register_admin_created' : 'register_no_approval_required';
      _user_mail_notify($op, $account);
      if ($notify) {
        drupal_set_message(t('A welcome message with further instructions has been e-mailed to the new user <a href="@url">%name</a>.', array(
          '@url' => url($uri['path'], $uri['options']),
          '%name' => $account->name,
        )));
      }
      else {
        drupal_set_message(t('A welcome message with further instructions has been sent to your e-mail address.'));
        $form_state['redirect'] = '';
      }
    }
  }
  else {
    _user_mail_notify('register_pending_approval', $account);
    drupal_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 e-mail address.'));
    $form_state['redirect'] = '';
  }
}