comparison core/modules/user/src/AccountForm.php @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents 129ea1e6d783
children
comparison
equal deleted inserted replaced
17:129ea1e6d783 18:af1871eacc83
66 $user = $this->currentUser(); 66 $user = $this->currentUser();
67 $config = \Drupal::config('user.settings'); 67 $config = \Drupal::config('user.settings');
68 $form['#cache']['tags'] = $config->getCacheTags(); 68 $form['#cache']['tags'] = $config->getCacheTags();
69 69
70 $language_interface = \Drupal::languageManager()->getCurrentLanguage(); 70 $language_interface = \Drupal::languageManager()->getCurrentLanguage();
71
72 // Check for new account.
71 $register = $account->isAnonymous(); 73 $register = $account->isAnonymous();
72 $admin = $user->hasPermission('administer users'); 74
75 // For a new account, there are 2 sub-cases:
76 // $self_register: A user creates their own, new, account
77 // (path '/user/register')
78 // $admin_create: An administrator creates a new account for another user
79 // (path '/admin/people/create')
80 // If the current user is logged in and has permission to create users
81 // then it must be the second case.
82 $admin_create = $register && $account->access('create');
83 $self_register = $register && !$admin_create;
73 84
74 // Account information. 85 // Account information.
75 $form['account'] = [ 86 $form['account'] = [
76 '#type' => 'container', 87 '#type' => 'container',
77 '#weight' => -10, 88 '#weight' => -10,
83 // Also see \Drupal\user\Plugin\Validation\Constraint\UserMailRequired. 94 // Also see \Drupal\user\Plugin\Validation\Constraint\UserMailRequired.
84 $form['account']['mail'] = [ 95 $form['account']['mail'] = [
85 '#type' => 'email', 96 '#type' => 'email',
86 '#title' => $this->t('Email address'), 97 '#title' => $this->t('Email address'),
87 '#description' => $this->t('A valid email address. All emails from the system will be sent to this address. The email address is not made public and will only be used if you wish to receive a new password or wish to receive certain news or notifications by email.'), 98 '#description' => $this->t('A valid email address. All emails from the system will be sent to this address. The email address is not made public and will only be used if you wish to receive a new password or wish to receive certain news or notifications by email.'),
88 '#required' => !(!$account->getEmail() && $admin), 99 '#required' => !(!$account->getEmail() && $user->hasPermission('administer users')),
89 '#default_value' => (!$register ? $account->getEmail() : ''), 100 '#default_value' => (!$register ? $account->getEmail() : ''),
90 ]; 101 ];
91 102
92 // Only show name field on registration form or user can change own username. 103 // Only show name field on registration form or user can change own username.
93 $form['account']['name'] = [ 104 $form['account']['name'] = [
94 '#type' => 'textfield', 105 '#type' => 'textfield',
95 '#title' => $this->t('Username'), 106 '#title' => $this->t('Username'),
96 '#maxlength' => USERNAME_MAX_LENGTH, 107 '#maxlength' => UserInterface::USERNAME_MAX_LENGTH,
97 '#description' => $this->t("Several special characters are allowed, including space, period (.), hyphen (-), apostrophe ('), underscore (_), and the @ sign."), 108 '#description' => $this->t("Several special characters are allowed, including space, period (.), hyphen (-), apostrophe ('), underscore (_), and the @ sign."),
98 '#required' => TRUE, 109 '#required' => TRUE,
99 '#attributes' => [ 110 '#attributes' => [
100 'class' => ['username'], 111 'class' => ['username'],
101 'autocorrect' => 'off', 112 'autocorrect' => 'off',
102 'autocapitalize' => 'off', 113 'autocapitalize' => 'off',
103 'spellcheck' => 'false', 114 'spellcheck' => 'false',
104 ], 115 ],
105 '#default_value' => (!$register ? $account->getAccountName() : ''), 116 '#default_value' => (!$register ? $account->getAccountName() : ''),
106 '#access' => ($register || ($user->id() == $account->id() && $user->hasPermission('change own username')) || $admin), 117 '#access' => $account->name->access('edit'),
107 ]; 118 ];
108 119
109 // Display password field only for existing users or when user is allowed to 120 // Display password field only for existing users or when user is allowed to
110 // assign a password during registration. 121 // assign a password during registration.
111 if (!$register) { 122 if (!$register) {
148 ':request_new_url' => $this->url('user.pass'), 159 ':request_new_url' => $this->url('user.pass'),
149 ]); 160 ]);
150 } 161 }
151 } 162 }
152 } 163 }
153 elseif (!$config->get('verify_mail') || $admin) { 164 elseif (!$config->get('verify_mail') || $admin_create) {
154 $form['account']['pass'] = [ 165 $form['account']['pass'] = [
155 '#type' => 'password_confirm', 166 '#type' => 'password_confirm',
156 '#size' => 25, 167 '#size' => 25,
157 '#description' => $this->t('Provide a password for the new account in both fields.'), 168 '#description' => $this->t('Provide a password for the new account in both fields.'),
158 '#required' => TRUE, 169 '#required' => TRUE,
167 $form['account'][$key]['#attributes']['autocomplete'] = 'off'; 178 $form['account'][$key]['#attributes']['autocomplete'] = 'off';
168 } 179 }
169 } 180 }
170 } 181 }
171 182
172 if ($admin || !$register) { 183 if (!$self_register) {
173 $status = $account->get('status')->value; 184 $status = $account->get('status')->value;
174 } 185 }
175 else { 186 else {
176 $status = $config->get('register') == USER_REGISTER_VISITORS ? 1 : 0; 187 $status = $config->get('register') == UserInterface::REGISTER_VISITORS ? 1 : 0;
177 } 188 }
178 189
179 $form['account']['status'] = [ 190 $form['account']['status'] = [
180 '#type' => 'radios', 191 '#type' => 'radios',
181 '#title' => $this->t('Status'), 192 '#title' => $this->t('Status'),
182 '#default_value' => $status, 193 '#default_value' => $status,
183 '#options' => [$this->t('Blocked'), $this->t('Active')], 194 '#options' => [$this->t('Blocked'), $this->t('Active')],
184 '#access' => $admin, 195 '#access' => $account->status->access('edit'),
185 ]; 196 ];
186 197
187 $roles = array_map(['\Drupal\Component\Utility\Html', 'escape'], user_role_names(TRUE)); 198 $roles = array_map(['\Drupal\Component\Utility\Html', 'escape'], user_role_names(TRUE));
188 199
189 $form['account']['roles'] = [ 200 $form['account']['roles'] = [
201 ]; 212 ];
202 213
203 $form['account']['notify'] = [ 214 $form['account']['notify'] = [
204 '#type' => 'checkbox', 215 '#type' => 'checkbox',
205 '#title' => $this->t('Notify user of new account'), 216 '#title' => $this->t('Notify user of new account'),
206 '#access' => $register && $admin, 217 '#access' => $admin_create,
207 ]; 218 ];
208 219
209 $user_preferred_langcode = $register ? $language_interface->getId() : $account->getPreferredLangcode(); 220 $user_preferred_langcode = $register ? $language_interface->getId() : $account->getPreferredLangcode();
210 221
211 $user_preferred_admin_langcode = $register ? $language_interface->getId() : $account->getPreferredAdminLangcode(FALSE); 222 $user_preferred_admin_langcode = $register ? $language_interface->getId() : $account->getPreferredAdminLangcode(FALSE);
220 '#type' => $this->languageManager->isMultilingual() ? 'details' : 'container', 231 '#type' => $this->languageManager->isMultilingual() ? 'details' : 'container',
221 '#title' => $this->t('Language settings'), 232 '#title' => $this->t('Language settings'),
222 '#open' => TRUE, 233 '#open' => TRUE,
223 // Display language selector when either creating a user on the admin 234 // Display language selector when either creating a user on the admin
224 // interface or editing a user account. 235 // interface or editing a user account.
225 '#access' => !$register || $admin, 236 '#access' => !$self_register,
226 ]; 237 ];
227 238
228 $form['language']['preferred_langcode'] = [ 239 $form['language']['preferred_langcode'] = [
229 '#type' => 'language_select', 240 '#type' => 'language_select',
230 '#title' => $this->t('Site language'), 241 '#title' => $this->t('Site language'),