5 use Drupal\Core\Form\FormStateInterface;
8 * Form handler for the user register forms.
10 class RegisterForm extends AccountForm {
15 public function form(array $form, FormStateInterface $form_state) {
16 $user = $this->currentUser();
17 /** @var \Drupal\user\UserInterface $account */
18 $account = $this->entity;
19 $admin = $user->hasPermission('administer users');
20 // Pass access information to the submit handler. Running an access check
21 // inside the submit function interferes with form processing and breaks
23 $form['administer_users'] = [
28 $form['#attached']['library'][] = 'core/drupal.form';
30 // For non-admin users, populate the form fields using data from the
33 $form['#attributes']['data-user-info-from-browser'] = TRUE;
36 // Because the user status has security implications, users are blocked by
37 // default when created programmatically and need to be actively activated
38 // if needed. When administrators create users from the user interface,
39 // however, we assume that they should be created as activated by default.
44 // Start with the default user account fields.
45 $form = parent::form($form, $form_state, $account);
53 protected function actions(array $form, FormStateInterface $form_state) {
54 $element = parent::actions($form, $form_state);
55 $element['submit']['#value'] = $this->t('Create new account');
62 public function submitForm(array &$form, FormStateInterface $form_state) {
63 $admin = $form_state->getValue('administer_users');
65 if (!\Drupal::config('user.settings')->get('verify_mail') || $admin) {
66 $pass = $form_state->getValue('pass');
69 $pass = user_password();
72 // Remove unneeded values.
73 $form_state->cleanValues();
75 $form_state->setValue('pass', $pass);
76 $form_state->setValue('init', $form_state->getValue('mail'));
78 parent::submitForm($form, $form_state);
84 public function save(array $form, FormStateInterface $form_state) {
85 $account = $this->entity;
86 $pass = $account->getPassword();
87 $admin = $form_state->getValue('administer_users');
88 $notify = !$form_state->isValueEmpty('notify');
90 // Save has no return value so this cannot be tested.
91 // Assume save has gone through correctly.
94 $form_state->set('user', $account);
95 $form_state->setValue('uid', $account->id());
97 $this->logger('user')->notice('New user: %name %email.', ['%name' => $form_state->getValue('name'), '%email' => '<' . $form_state->getValue('mail') . '>', 'type' => $account->link($this->t('Edit'), 'edit-form')]);
99 // Add plain text password into user account to generate mail tokens.
100 $account->password = $pass;
102 // New administrative account without notification.
103 if ($admin && !$notify) {
104 drupal_set_message($this->t('Created a new user account for <a href=":url">%name</a>. No email has been sent.', [':url' => $account->url(), '%name' => $account->getUsername()]));
106 // No email verification required; log in user immediately.
107 elseif (!$admin && !\Drupal::config('user.settings')->get('verify_mail') && $account->isActive()) {
108 _user_mail_notify('register_no_approval_required', $account);
109 user_login_finalize($account);
110 drupal_set_message($this->t('Registration successful. You are now logged in.'));
111 $form_state->setRedirect('<front>');
113 // No administrator approval required.
114 elseif ($account->isActive() || $notify) {
115 if (!$account->getEmail() && $notify) {
116 drupal_set_message($this->t('The new user <a href=":url">%name</a> was created without an email address, so no welcome message was sent.', [':url' => $account->url(), '%name' => $account->getUsername()]));
119 $op = $notify ? 'register_admin_created' : 'register_no_approval_required';
120 if (_user_mail_notify($op, $account)) {
122 drupal_set_message($this->t('A welcome message with further instructions has been emailed to the new user <a href=":url">%name</a>.', [':url' => $account->url(), '%name' => $account->getUsername()]));
125 drupal_set_message($this->t('A welcome message with further instructions has been sent to your email address.'));
126 $form_state->setRedirect('<front>');
131 // Administrator approval required.
133 _user_mail_notify('register_pending_approval', $account);
134 drupal_set_message($this->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.'));
135 $form_state->setRedirect('<front>');