5 use Drupal\Core\Form\ConfigFormBase;
6 use Drupal\Core\Config\ConfigFactoryInterface;
7 use Drupal\Core\Extension\ModuleHandlerInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Render\Element;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
13 * Configure user settings for this site.
15 class AccountSettingsForm extends ConfigFormBase {
20 * @var \Drupal\Core\Extension\ModuleHandlerInterface
22 protected $moduleHandler;
25 * The role storage used when changing the admin role.
27 * @var \Drupal\user\RoleStorageInterface
29 protected $roleStorage;
32 * Constructs a \Drupal\user\AccountSettingsForm object.
34 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
35 * The factory for configuration objects.
36 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
38 * @param \Drupal\user\RoleStorageInterface $role_storage
41 public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, RoleStorageInterface $role_storage) {
42 parent::__construct($config_factory);
43 $this->moduleHandler = $module_handler;
44 $this->roleStorage = $role_storage;
50 public static function create(ContainerInterface $container) {
52 $container->get('config.factory'),
53 $container->get('module_handler'),
54 $container->get('entity.manager')->getStorage('user_role')
61 public function getFormId() {
62 return 'user_admin_settings';
68 protected function getEditableConfigNames() {
79 public function buildForm(array $form, FormStateInterface $form_state) {
80 $form = parent::buildForm($form, $form_state);
81 $config = $this->config('user.settings');
82 $mail_config = $this->config('user.mail');
83 $site_config = $this->config('system.site');
85 $form['#attached']['library'][] = 'user/drupal.user.admin';
87 // Settings for anonymous users.
88 $form['anonymous_settings'] = [
90 '#title' => $this->t('Anonymous users'),
93 $form['anonymous_settings']['anonymous'] = [
94 '#type' => 'textfield',
95 '#title' => $this->t('Name'),
96 '#default_value' => $config->get('anonymous'),
97 '#description' => $this->t('The name used to indicate anonymous users.'),
101 // Administrative role option.
102 $form['admin_role'] = [
103 '#type' => 'details',
104 '#title' => $this->t('Administrator role'),
107 // Do not allow users to set the anonymous or authenticated user roles as the
108 // administrator role.
109 $roles = user_role_names(TRUE);
110 unset($roles[RoleInterface::AUTHENTICATED_ID]);
112 $admin_roles = $this->roleStorage->getQuery()
113 ->condition('is_admin', TRUE)
115 $default_value = reset($admin_roles);
117 $form['admin_role']['user_admin_role'] = [
119 '#title' => $this->t('Administrator role'),
120 '#empty_value' => '',
121 '#default_value' => $default_value,
122 '#options' => $roles,
123 '#description' => $this->t('This role will be automatically assigned new permissions whenever a module is enabled. Changing this setting will not affect existing permissions.'),
124 // Don't allow to select a single admin role in case multiple roles got
125 // marked as admin role already.
126 '#access' => count($admin_roles) <= 1,
129 // @todo Remove this check once language settings are generalized.
130 if ($this->moduleHandler->moduleExists('content_translation')) {
131 $form['language'] = [
132 '#type' => 'details',
133 '#title' => $this->t('Language settings'),
137 $form_state->set(['content_translation', 'key'], 'language');
138 $form['language'] += content_translation_enable_widget('user', 'user', $form, $form_state);
141 // User registration settings.
142 $form['registration_cancellation'] = [
143 '#type' => 'details',
144 '#title' => $this->t('Registration and cancellation'),
147 $form['registration_cancellation']['user_register'] = [
149 '#title' => $this->t('Who can register accounts?'),
150 '#default_value' => $config->get('register'),
152 USER_REGISTER_ADMINISTRATORS_ONLY => $this->t('Administrators only'),
153 USER_REGISTER_VISITORS => $this->t('Visitors'),
154 USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL => $this->t('Visitors, but administrator approval is required'),
157 $form['registration_cancellation']['user_email_verification'] = [
158 '#type' => 'checkbox',
159 '#title' => $this->t('Require email verification when a visitor creates an account'),
160 '#default_value' => $config->get('verify_mail'),
161 '#description' => $this->t('New users will be required to validate their email address prior to logging into the site, and will be assigned a system-generated password. With this setting disabled, users will be logged in immediately upon registering, and may select their own passwords during registration.')
163 $form['registration_cancellation']['user_password_strength'] = [
164 '#type' => 'checkbox',
165 '#title' => $this->t('Enable password strength indicator'),
166 '#default_value' => $config->get('password_strength'),
168 $form['registration_cancellation']['user_cancel_method'] = [
170 '#title' => $this->t('When cancelling a user account'),
171 '#default_value' => $config->get('cancel_method'),
172 '#description' => $this->t('Users with the %select-cancel-method or %administer-users <a href=":permissions-url">permissions</a> can override this default method.', ['%select-cancel-method' => $this->t('Select method for cancelling account'), '%administer-users' => $this->t('Administer users'), ':permissions-url' => $this->url('user.admin_permissions')]),
174 $form['registration_cancellation']['user_cancel_method'] += user_cancel_methods();
175 foreach (Element::children($form['registration_cancellation']['user_cancel_method']) as $key) {
176 // All account cancellation methods that specify #access cannot be
177 // configured as default method.
178 // @see hook_user_cancel_methods_alter()
179 if (isset($form['registration_cancellation']['user_cancel_method'][$key]['#access'])) {
180 $form['registration_cancellation']['user_cancel_method'][$key]['#access'] = FALSE;
184 // Default notifications address.
185 $form['mail_notification_address'] = [
187 '#title' => $this->t('Notification email address'),
188 '#default_value' => $site_config->get('mail_notification'),
189 '#description' => $this->t("The email address to be used as the 'from' address for all account notifications listed below. If <em>'Visitors, but administrator approval is required'</em> is selected above, a notification email will also be sent to this address for any new registrations. Leave empty to use the default system email address <em>(%site-email).</em>", ['%site-email' => $site_config->get('mail')]),
194 '#type' => 'vertical_tabs',
195 '#title' => $this->t('Emails'),
197 // These email tokens are shared for all settings, so just define
198 // the list once to help ensure they stay in sync.
199 $email_token_help = $this->t('Available variables are: [site:name], [site:url], [user:display-name], [user:account-name], [user:mail], [site:login-url], [site:url-brief], [user:edit-url], [user:one-time-login-url], [user:cancel-url].');
201 $form['email_admin_created'] = [
202 '#type' => 'details',
203 '#title' => $this->t('Welcome (new user created by administrator)'),
204 '#open' => $config->get('register') == USER_REGISTER_ADMINISTRATORS_ONLY,
205 '#description' => $this->t('Edit the welcome email messages sent to new member accounts created by an administrator.') . ' ' . $email_token_help,
208 $form['email_admin_created']['user_mail_register_admin_created_subject'] = [
209 '#type' => 'textfield',
210 '#title' => $this->t('Subject'),
211 '#default_value' => $mail_config->get('register_admin_created.subject'),
214 $form['email_admin_created']['user_mail_register_admin_created_body'] = [
215 '#type' => 'textarea',
216 '#title' => $this->t('Body'),
217 '#default_value' => $mail_config->get('register_admin_created.body'),
221 $form['email_pending_approval'] = [
222 '#type' => 'details',
223 '#title' => $this->t('Welcome (awaiting approval)'),
224 '#open' => $config->get('register') == USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL,
225 '#description' => $this->t('Edit the welcome email messages sent to new members upon registering, when administrative approval is required.') . ' ' . $email_token_help,
228 $form['email_pending_approval']['user_mail_register_pending_approval_subject'] = [
229 '#type' => 'textfield',
230 '#title' => $this->t('Subject'),
231 '#default_value' => $mail_config->get('register_pending_approval.subject'),
234 $form['email_pending_approval']['user_mail_register_pending_approval_body'] = [
235 '#type' => 'textarea',
236 '#title' => $this->t('Body'),
237 '#default_value' => $mail_config->get('register_pending_approval.body'),
241 $form['email_pending_approval_admin'] = [
242 '#type' => 'details',
243 '#title' => $this->t('Admin (user awaiting approval)'),
244 '#open' => $config->get('register') == USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL,
245 '#description' => $this->t('Edit the email notifying the site administrator that there are new members awaiting administrative approval.') . ' ' . $email_token_help,
248 $form['email_pending_approval_admin']['register_pending_approval_admin_subject'] = [
249 '#type' => 'textfield',
250 '#title' => $this->t('Subject'),
251 '#default_value' => $mail_config->get('register_pending_approval_admin.subject'),
254 $form['email_pending_approval_admin']['register_pending_approval_admin_body'] = [
255 '#type' => 'textarea',
256 '#title' => $this->t('Body'),
257 '#default_value' => $mail_config->get('register_pending_approval_admin.body'),
261 $form['email_no_approval_required'] = [
262 '#type' => 'details',
263 '#title' => $this->t('Welcome (no approval required)'),
264 '#open' => $config->get('register') == USER_REGISTER_VISITORS,
265 '#description' => $this->t('Edit the welcome email messages sent to new members upon registering, when no administrator approval is required.') . ' ' . $email_token_help,
268 $form['email_no_approval_required']['user_mail_register_no_approval_required_subject'] = [
269 '#type' => 'textfield',
270 '#title' => $this->t('Subject'),
271 '#default_value' => $mail_config->get('register_no_approval_required.subject'),
274 $form['email_no_approval_required']['user_mail_register_no_approval_required_body'] = [
275 '#type' => 'textarea',
276 '#title' => $this->t('Body'),
277 '#default_value' => $mail_config->get('register_no_approval_required.body'),
281 $form['email_password_reset'] = [
282 '#type' => 'details',
283 '#title' => $this->t('Password recovery'),
284 '#description' => $this->t('Edit the email messages sent to users who request a new password.') . ' ' . $email_token_help,
288 $form['email_password_reset']['user_mail_password_reset_subject'] = [
289 '#type' => 'textfield',
290 '#title' => $this->t('Subject'),
291 '#default_value' => $mail_config->get('password_reset.subject'),
294 $form['email_password_reset']['user_mail_password_reset_body'] = [
295 '#type' => 'textarea',
296 '#title' => $this->t('Body'),
297 '#default_value' => $mail_config->get('password_reset.body'),
301 $form['email_activated'] = [
302 '#type' => 'details',
303 '#title' => $this->t('Account activation'),
304 '#description' => $this->t('Enable and edit email messages sent to users upon account activation (when an administrator activates an account of a user who has already registered, on a site where administrative approval is required).') . ' ' . $email_token_help,
307 $form['email_activated']['user_mail_status_activated_notify'] = [
308 '#type' => 'checkbox',
309 '#title' => $this->t('Notify user when account is activated'),
310 '#default_value' => $config->get('notify.status_activated'),
312 $form['email_activated']['settings'] = [
313 '#type' => 'container',
315 // Hide the additional settings when this email is disabled.
317 'input[name="user_mail_status_activated_notify"]' => ['checked' => FALSE],
321 $form['email_activated']['settings']['user_mail_status_activated_subject'] = [
322 '#type' => 'textfield',
323 '#title' => $this->t('Subject'),
324 '#default_value' => $mail_config->get('status_activated.subject'),
327 $form['email_activated']['settings']['user_mail_status_activated_body'] = [
328 '#type' => 'textarea',
329 '#title' => $this->t('Body'),
330 '#default_value' => $mail_config->get('status_activated.body'),
334 $form['email_blocked'] = [
335 '#type' => 'details',
336 '#title' => $this->t('Account blocked'),
337 '#description' => $this->t('Enable and edit email messages sent to users when their accounts are blocked.') . ' ' . $email_token_help,
340 $form['email_blocked']['user_mail_status_blocked_notify'] = [
341 '#type' => 'checkbox',
342 '#title' => $this->t('Notify user when account is blocked'),
343 '#default_value' => $config->get('notify.status_blocked'),
345 $form['email_blocked']['settings'] = [
346 '#type' => 'container',
348 // Hide the additional settings when the blocked email is disabled.
350 'input[name="user_mail_status_blocked_notify"]' => ['checked' => FALSE],
354 $form['email_blocked']['settings']['user_mail_status_blocked_subject'] = [
355 '#type' => 'textfield',
356 '#title' => $this->t('Subject'),
357 '#default_value' => $mail_config->get('status_blocked.subject'),
360 $form['email_blocked']['settings']['user_mail_status_blocked_body'] = [
361 '#type' => 'textarea',
362 '#title' => $this->t('Body'),
363 '#default_value' => $mail_config->get('status_blocked.body'),
367 $form['email_cancel_confirm'] = [
368 '#type' => 'details',
369 '#title' => $this->t('Account cancellation confirmation'),
370 '#description' => $this->t('Edit the email messages sent to users when they attempt to cancel their accounts.') . ' ' . $email_token_help,
373 $form['email_cancel_confirm']['user_mail_cancel_confirm_subject'] = [
374 '#type' => 'textfield',
375 '#title' => $this->t('Subject'),
376 '#default_value' => $mail_config->get('cancel_confirm.subject'),
379 $form['email_cancel_confirm']['user_mail_cancel_confirm_body'] = [
380 '#type' => 'textarea',
381 '#title' => $this->t('Body'),
382 '#default_value' => $mail_config->get('cancel_confirm.body'),
386 $form['email_canceled'] = [
387 '#type' => 'details',
388 '#title' => $this->t('Account canceled'),
389 '#description' => $this->t('Enable and edit email messages sent to users when their accounts are canceled.') . ' ' . $email_token_help,
392 $form['email_canceled']['user_mail_status_canceled_notify'] = [
393 '#type' => 'checkbox',
394 '#title' => $this->t('Notify user when account is canceled'),
395 '#default_value' => $config->get('notify.status_canceled'),
397 $form['email_canceled']['settings'] = [
398 '#type' => 'container',
400 // Hide the settings when the cancel notify checkbox is disabled.
402 'input[name="user_mail_status_canceled_notify"]' => ['checked' => FALSE],
406 $form['email_canceled']['settings']['user_mail_status_canceled_subject'] = [
407 '#type' => 'textfield',
408 '#title' => $this->t('Subject'),
409 '#default_value' => $mail_config->get('status_canceled.subject'),
412 $form['email_canceled']['settings']['user_mail_status_canceled_body'] = [
413 '#type' => 'textarea',
414 '#title' => $this->t('Body'),
415 '#default_value' => $mail_config->get('status_canceled.body'),
425 public function submitForm(array &$form, FormStateInterface $form_state) {
426 parent::submitForm($form, $form_state);
428 $this->config('user.settings')
429 ->set('anonymous', $form_state->getValue('anonymous'))
430 ->set('register', $form_state->getValue('user_register'))
431 ->set('password_strength', $form_state->getValue('user_password_strength'))
432 ->set('verify_mail', $form_state->getValue('user_email_verification'))
433 ->set('cancel_method', $form_state->getValue('user_cancel_method'))
434 ->set('notify.status_activated', $form_state->getValue('user_mail_status_activated_notify'))
435 ->set('notify.status_blocked', $form_state->getValue('user_mail_status_blocked_notify'))
436 ->set('notify.status_canceled', $form_state->getValue('user_mail_status_canceled_notify'))
438 $this->config('user.mail')
439 ->set('cancel_confirm.body', $form_state->getValue('user_mail_cancel_confirm_body'))
440 ->set('cancel_confirm.subject', $form_state->getValue('user_mail_cancel_confirm_subject'))
441 ->set('password_reset.body', $form_state->getValue('user_mail_password_reset_body'))
442 ->set('password_reset.subject', $form_state->getValue('user_mail_password_reset_subject'))
443 ->set('register_admin_created.body', $form_state->getValue('user_mail_register_admin_created_body'))
444 ->set('register_admin_created.subject', $form_state->getValue('user_mail_register_admin_created_subject'))
445 ->set('register_no_approval_required.body', $form_state->getValue('user_mail_register_no_approval_required_body'))
446 ->set('register_no_approval_required.subject', $form_state->getValue('user_mail_register_no_approval_required_subject'))
447 ->set('register_pending_approval.body', $form_state->getValue('user_mail_register_pending_approval_body'))
448 ->set('register_pending_approval.subject', $form_state->getValue('user_mail_register_pending_approval_subject'))
449 ->set('register_pending_approval_admin.body', $form_state->getValue('register_pending_approval_admin_body'))
450 ->set('register_pending_approval_admin.subject', $form_state->getValue('register_pending_approval_admin_subject'))
451 ->set('status_activated.body', $form_state->getValue('user_mail_status_activated_body'))
452 ->set('status_activated.subject', $form_state->getValue('user_mail_status_activated_subject'))
453 ->set('status_blocked.body', $form_state->getValue('user_mail_status_blocked_body'))
454 ->set('status_blocked.subject', $form_state->getValue('user_mail_status_blocked_subject'))
455 ->set('status_canceled.body', $form_state->getValue('user_mail_status_canceled_body'))
456 ->set('status_canceled.subject', $form_state->getValue('user_mail_status_canceled_subject'))
458 $this->config('system.site')
459 ->set('mail_notification', $form_state->getValue('mail_notification_address'))
462 // Change the admin role.
463 if ($form_state->hasValue('user_admin_role')) {
464 $admin_roles = $this->roleStorage->getQuery()
465 ->condition('is_admin', TRUE)
468 foreach ($admin_roles as $rid) {
469 $this->roleStorage->load($rid)->setIsAdmin(FALSE)->save();
472 $new_admin_role = $form_state->getValue('user_admin_role');
473 if ($new_admin_role) {
474 $this->roleStorage->load($new_admin_role)->setIsAdmin(TRUE)->save();