7a97974fb2ddf05514f6b39258dee21a32b7a7c0
[yaffs-website] / web / core / modules / user / src / RegisterForm.php
1 <?php
2
3 namespace Drupal\user;
4
5 use Drupal\Core\Form\FormStateInterface;
6
7 /**
8  * Form handler for the user register forms.
9  */
10 class RegisterForm extends AccountForm {
11
12   /**
13    * {@inheritdoc}
14    */
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
22     // hook_form_alter().
23     $form['administer_users'] = [
24       '#type' => 'value',
25       '#value' => $admin,
26     ];
27
28     $form['#attached']['library'][] = 'core/drupal.form';
29
30     // For non-admin users, populate the form fields using data from the
31     // browser.
32     if (!$admin) {
33       $form['#attributes']['data-user-info-from-browser'] = TRUE;
34     }
35
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.
40     if ($admin) {
41       $account->activate();
42     }
43
44     // Start with the default user account fields.
45     $form = parent::form($form, $form_state, $account);
46
47     return $form;
48   }
49
50   /**
51    * {@inheritdoc}
52    */
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');
56     return $element;
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public function submitForm(array &$form, FormStateInterface $form_state) {
63     $admin = $form_state->getValue('administer_users');
64
65     if (!\Drupal::config('user.settings')->get('verify_mail') || $admin) {
66       $pass = $form_state->getValue('pass');
67     }
68     else {
69       $pass = user_password();
70     }
71
72     // Remove unneeded values.
73     $form_state->cleanValues();
74
75     $form_state->setValue('pass', $pass);
76     $form_state->setValue('init', $form_state->getValue('mail'));
77
78     parent::submitForm($form, $form_state);
79   }
80
81   /**
82    * {@inheritdoc}
83    */
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');
89
90     // Save has no return value so this cannot be tested.
91     // Assume save has gone through correctly.
92     $account->save();
93
94     $form_state->set('user', $account);
95     $form_state->setValue('uid', $account->id());
96
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')]);
98
99     // Add plain text password into user account to generate mail tokens.
100     $account->password = $pass;
101
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()]));
105     }
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>');
112     }
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()]));
117       }
118       else {
119         $op = $notify ? 'register_admin_created' : 'register_no_approval_required';
120         if (_user_mail_notify($op, $account)) {
121           if ($notify) {
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()]));
123           }
124           else {
125             drupal_set_message($this->t('A welcome message with further instructions has been sent to your email address.'));
126             $form_state->setRedirect('<front>');
127           }
128         }
129       }
130     }
131     // Administrator approval required.
132     else {
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>');
136     }
137   }
138
139 }