RegisterForm.php 5.42 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\user;

5
use Drupal\Core\Form\FormStateInterface;
6

7
/**
8
 * Form handler for the user register forms.
9 10
 *
 * @internal
11
 */
12
class RegisterForm extends AccountForm {
13 14

  /**
15
   * {@inheritdoc}
16
   */
17
  public function form(array $form, FormStateInterface $form_state) {
18
    $user = $this->currentUser();
19
    /** @var \Drupal\user\UserInterface $account */
20
    $account = $this->entity;
21 22 23 24 25 26 27 28

    // This form is used for two cases:
    // - Self-register (route = 'user.register').
    // - Admin-create (route = 'user.admin_create').
    // If the current user has permission to create users then it must be the
    // second case.
    $admin = $account->access('create');

29 30 31
    // Pass access information to the submit handler. Running an access check
    // inside the submit function interferes with form processing and breaks
    // hook_form_alter().
32
    $form['administer_users'] = [
33 34
      '#type' => 'value',
      '#value' => $admin,
35
    ];
36

37
    $form['#attached']['library'][] = 'core/drupal.form';
38 39 40 41 42 43

    // For non-admin users, populate the form fields using data from the
    // browser.
    if (!$admin) {
      $form['#attributes']['data-user-info-from-browser'] = TRUE;
    }
44

45 46 47 48 49 50 51 52
    // Because the user status has security implications, users are blocked by
    // default when created programmatically and need to be actively activated
    // if needed. When administrators create users from the user interface,
    // however, we assume that they should be created as activated by default.
    if ($admin) {
      $account->activate();
    }

53 54 55 56 57 58 59
    // Start with the default user account fields.
    $form = parent::form($form, $form_state, $account);

    return $form;
  }

  /**
60
   * {@inheritdoc}
61
   */
62
  protected function actions(array $form, FormStateInterface $form_state) {
63
    $element = parent::actions($form, $form_state);
64
    $element['submit']['#value'] = $this->t('Create new account');
65 66 67 68
    return $element;
  }

  /**
69
   * {@inheritdoc}
70
   */
71
  public function submitForm(array &$form, FormStateInterface $form_state) {
72
    $admin = $form_state->getValue('administer_users');
73

74
    if (!\Drupal::config('user.settings')->get('verify_mail') || $admin) {
75
      $pass = $form_state->getValue('pass');
76 77 78 79 80 81
    }
    else {
      $pass = user_password();
    }

    // Remove unneeded values.
82
    $form_state->cleanValues();
83

84 85
    $form_state->setValue('pass', $pass);
    $form_state->setValue('init', $form_state->getValue('mail'));
86

87
    parent::submitForm($form, $form_state);
88 89 90
  }

  /**
91
   * {@inheritdoc}
92
   */
93
  public function save(array $form, FormStateInterface $form_state) {
94
    $account = $this->entity;
95
    $pass = $account->getPassword();
96 97
    $admin = $form_state->getValue('administer_users');
    $notify = !$form_state->isValueEmpty('notify');
98

99 100
    // Save has no return value so this cannot be tested.
    // Assume save has gone through correctly.
101 102
    $account->save();

103
    $form_state->set('user', $account);
104
    $form_state->setValue('uid', $account->id());
105

106
    $this->logger('user')->notice('New user: %name %email.', ['%name' => $form_state->getValue('name'), '%email' => '<' . $form_state->getValue('mail') . '>', 'type' => $account->toLink($this->t('Edit'), 'edit-form')->toString()]);
107 108 109 110 111 112

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

    // New administrative account without notification.
    if ($admin && !$notify) {
113
      $this->messenger()->addStatus($this->t('Created a new user account for <a href=":url">%name</a>. No email has been sent.', [':url' => $account->toUrl()->toString(), '%name' => $account->getAccountName()]));
114
    }
115
    // No email verification required; log in user immediately.
116
    elseif (!$admin && !\Drupal::config('user.settings')->get('verify_mail') && $account->isActive()) {
117
      _user_mail_notify('register_no_approval_required', $account);
118
      user_login_finalize($account);
119
      $this->messenger()->addStatus($this->t('Registration successful. You are now logged in.'));
120
      $form_state->setRedirect('<front>');
121 122
    }
    // No administrator approval required.
123 124
    elseif ($account->isActive() || $notify) {
      if (!$account->getEmail() && $notify) {
125
        $this->messenger()->addStatus($this->t('The new user <a href=":url">%name</a> was created without an email address, so no welcome message was sent.', [':url' => $account->toUrl()->toString(), '%name' => $account->getAccountName()]));
126 127 128
      }
      else {
        $op = $notify ? 'register_admin_created' : 'register_no_approval_required';
129 130
        if (_user_mail_notify($op, $account)) {
          if ($notify) {
131
            $this->messenger()->addStatus($this->t('A welcome message with further instructions has been emailed to the new user <a href=":url">%name</a>.', [':url' => $account->toUrl()->toString(), '%name' => $account->getAccountName()]));
132 133
          }
          else {
134
            $this->messenger()->addStatus($this->t('A welcome message with further instructions has been sent to your email address.'));
135
            $form_state->setRedirect('<front>');
136
          }
137 138 139 140 141 142
        }
      }
    }
    // Administrator approval required.
    else {
      _user_mail_notify('register_pending_approval', $account);
143
      $this->messenger()->addStatus($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.'));
144
      $form_state->setRedirect('<front>');
145 146
    }
  }
147

148
}