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.
17 class AccountSettingsForm extends ConfigFormBase {
22 * @var \Drupal\Core\Extension\ModuleHandlerInterface
24 protected $moduleHandler;
27 * The role storage used when changing the admin role.
29 * @var \Drupal\user\RoleStorageInterface
31 protected $roleStorage;
34 * Constructs a \Drupal\user\AccountSettingsForm object.
36 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
37 * The factory for configuration objects.
38 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
40 * @param \Drupal\user\RoleStorageInterface $role_storage
43 public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, RoleStorageInterface $role_storage) {
44 parent::__construct($config_factory);
45 $this->moduleHandler = $module_handler;
46 $this->roleStorage = $role_storage;
52 public static function create(ContainerInterface $container) {
54 $container->get('config.factory'),
55 $container->get('module_handler'),
56 $container->get('entity.manager')->getStorage('user_role')
63 public function getFormId() {
64 return 'user_admin_settings';
70 protected function getEditableConfigNames() {
81 public function buildForm(array $form, FormStateInterface $form_state) {
82 $form = parent::buildForm($form, $form_state);
83 $config = $this->config('user.settings');
84 $mail_config = $this->config('user.mail');
85 $site_config = $this->config('system.site');
87 $form['#attached']['library'][] = 'user/drupal.user.admin';
89 // Settings for anonymous users.
90 $form['anonymous_settings'] = [
92 '#title' => $this->t('Anonymous users'),
95 $form['anonymous_settings']['anonymous'] = [
96 '#type' => 'textfield',
97 '#title' => $this->t('Name'),
98 '#default_value' => $config->get('anonymous'),
99 '#description' => $this->t('The name used to indicate anonymous users.'),
103 // Administrative role option.
104 $form['admin_role'] = [
105 '#type' => 'details',
106 '#title' => $this->t('Administrator role'),
109 // Do not allow users to set the anonymous or authenticated user roles as the
110 // administrator role.
111 $roles = user_role_names(TRUE);
112 unset($roles[RoleInterface::AUTHENTICATED_ID]);
114 $admin_roles = $this->roleStorage->getQuery()
115 ->condition('is_admin', TRUE)
117 $default_value = reset($admin_roles);
119 $form['admin_role']['user_admin_role'] = [
121 '#title' => $this->t('Administrator role'),
122 '#empty_value' => '',
123 '#default_value' => $default_value,
124 '#options' => $roles,
125 '#description' => $this->t('This role will be automatically assigned new permissions whenever a module is enabled. Changing this setting will not affect existing permissions.'),
126 // Don't allow to select a single admin role in case multiple roles got
127 // marked as admin role already.
128 '#access' => count($admin_roles) <= 1,
131 // @todo Remove this check once language settings are generalized.
132 if ($this->moduleHandler->moduleExists('content_translation')) {
133 $form['language'] = [
134 '#type' => 'details',
135 '#title' => $this->t('Language settings'),
139 $form_state->set(['content_translation', 'key'], 'language');
140 $form['language'] += content_translation_enable_widget('user', 'user', $form, $form_state);
143 // User registration settings.
144 $form['registration_cancellation'] = [
145 '#type' => 'details',
146 '#title' => $this->t('Registration and cancellation'),
149 $form['registration_cancellation']['user_register'] = [
151 '#title' => $this->t('Who can register accounts?'),
152 '#default_value' => $config->get('register'),
154 USER_REGISTER_ADMINISTRATORS_ONLY => $this->t('Administrators only'),
155 USER_REGISTER_VISITORS => $this->t('Visitors'),
156 USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL => $this->t('Visitors, but administrator approval is required'),
159 $form['registration_cancellation']['user_email_verification'] = [
160 '#type' => 'checkbox',
161 '#title' => $this->t('Require email verification when a visitor creates an account'),
162 '#default_value' => $config->get('verify_mail'),
163 '#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.')
165 $form['registration_cancellation']['user_password_strength'] = [
166 '#type' => 'checkbox',
167 '#title' => $this->t('Enable password strength indicator'),
168 '#default_value' => $config->get('password_strength'),
170 $form['registration_cancellation']['user_cancel_method'] = [
172 '#title' => $this->t('When cancelling a user account'),
173 '#default_value' => $config->get('cancel_method'),
174 '#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')]),
176 $form['registration_cancellation']['user_cancel_method'] += user_cancel_methods();
177 foreach (Element::children($form['registration_cancellation']['user_cancel_method']) as $key) {
178 // All account cancellation methods that specify #access cannot be
179 // configured as default method.
180 // @see hook_user_cancel_methods_alter()
181 if (isset($form['registration_cancellation']['user_cancel_method'][$key]['#access'])) {
182 $form['registration_cancellation']['user_cancel_method'][$key]['#access'] = FALSE;
186 // Default notifications address.
187 $form['mail_notification_address'] = [
189 '#title' => $this->t('Notification email address'),
190 '#default_value' => $site_config->get('mail_notification'),
191 '#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')]),
196 '#type' => 'vertical_tabs',
197 '#title' => $this->t('Emails'),
199 // These email tokens are shared for all settings, so just define
200 // the list once to help ensure they stay in sync.
201 $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].');
203 $form['email_admin_created'] = [
204 '#type' => 'details',
205 '#title' => $this->t('Welcome (new user created by administrator)'),
206 '#open' => $config->get('register') == USER_REGISTER_ADMINISTRATORS_ONLY,
207 '#description' => $this->t('Edit the welcome email messages sent to new member accounts created by an administrator.') . ' ' . $email_token_help,
210 $form['email_admin_created']['user_mail_register_admin_created_subject'] = [
211 '#type' => 'textfield',
212 '#title' => $this->t('Subject'),
213 '#default_value' => $mail_config->get('register_admin_created.subject'),
216 $form['email_admin_created']['user_mail_register_admin_created_body'] = [
217 '#type' => 'textarea',
218 '#title' => $this->t('Body'),
219 '#default_value' => $mail_config->get('register_admin_created.body'),
223 $form['email_pending_approval'] = [
224 '#type' => 'details',
225 '#title' => $this->t('Welcome (awaiting approval)'),
226 '#open' => $config->get('register') == USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL,
227 '#description' => $this->t('Edit the welcome email messages sent to new members upon registering, when administrative approval is required.') . ' ' . $email_token_help,
230 $form['email_pending_approval']['user_mail_register_pending_approval_subject'] = [
231 '#type' => 'textfield',
232 '#title' => $this->t('Subject'),
233 '#default_value' => $mail_config->get('register_pending_approval.subject'),
236 $form['email_pending_approval']['user_mail_register_pending_approval_body'] = [
237 '#type' => 'textarea',
238 '#title' => $this->t('Body'),
239 '#default_value' => $mail_config->get('register_pending_approval.body'),
243 $form['email_pending_approval_admin'] = [
244 '#type' => 'details',
245 '#title' => $this->t('Admin (user awaiting approval)'),
246 '#open' => $config->get('register') == USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL,
247 '#description' => $this->t('Edit the email notifying the site administrator that there are new members awaiting administrative approval.') . ' ' . $email_token_help,
250 $form['email_pending_approval_admin']['register_pending_approval_admin_subject'] = [
251 '#type' => 'textfield',
252 '#title' => $this->t('Subject'),
253 '#default_value' => $mail_config->get('register_pending_approval_admin.subject'),
256 $form['email_pending_approval_admin']['register_pending_approval_admin_body'] = [
257 '#type' => 'textarea',
258 '#title' => $this->t('Body'),
259 '#default_value' => $mail_config->get('register_pending_approval_admin.body'),
263 $form['email_no_approval_required'] = [
264 '#type' => 'details',
265 '#title' => $this->t('Welcome (no approval required)'),
266 '#open' => $config->get('register') == USER_REGISTER_VISITORS,
267 '#description' => $this->t('Edit the welcome email messages sent to new members upon registering, when no administrator approval is required.') . ' ' . $email_token_help,
270 $form['email_no_approval_required']['user_mail_register_no_approval_required_subject'] = [
271 '#type' => 'textfield',
272 '#title' => $this->t('Subject'),
273 '#default_value' => $mail_config->get('register_no_approval_required.subject'),
276 $form['email_no_approval_required']['user_mail_register_no_approval_required_body'] = [
277 '#type' => 'textarea',
278 '#title' => $this->t('Body'),
279 '#default_value' => $mail_config->get('register_no_approval_required.body'),
283 $form['email_password_reset'] = [
284 '#type' => 'details',
285 '#title' => $this->t('Password recovery'),
286 '#description' => $this->t('Edit the email messages sent to users who request a new password.') . ' ' . $email_token_help,
290 $form['email_password_reset']['user_mail_password_reset_subject'] = [
291 '#type' => 'textfield',
292 '#title' => $this->t('Subject'),
293 '#default_value' => $mail_config->get('password_reset.subject'),
296 $form['email_password_reset']['user_mail_password_reset_body'] = [
297 '#type' => 'textarea',
298 '#title' => $this->t('Body'),
299 '#default_value' => $mail_config->get('password_reset.body'),
303 $form['email_activated'] = [
304 '#type' => 'details',
305 '#title' => $this->t('Account activation'),
306 '#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,
309 $form['email_activated']['user_mail_status_activated_notify'] = [
310 '#type' => 'checkbox',
311 '#title' => $this->t('Notify user when account is activated'),
312 '#default_value' => $config->get('notify.status_activated'),
314 $form['email_activated']['settings'] = [
315 '#type' => 'container',
317 // Hide the additional settings when this email is disabled.
319 'input[name="user_mail_status_activated_notify"]' => ['checked' => FALSE],
323 $form['email_activated']['settings']['user_mail_status_activated_subject'] = [
324 '#type' => 'textfield',
325 '#title' => $this->t('Subject'),
326 '#default_value' => $mail_config->get('status_activated.subject'),
329 $form['email_activated']['settings']['user_mail_status_activated_body'] = [
330 '#type' => 'textarea',
331 '#title' => $this->t('Body'),
332 '#default_value' => $mail_config->get('status_activated.body'),
336 $form['email_blocked'] = [
337 '#type' => 'details',
338 '#title' => $this->t('Account blocked'),
339 '#description' => $this->t('Enable and edit email messages sent to users when their accounts are blocked.') . ' ' . $email_token_help,
342 $form['email_blocked']['user_mail_status_blocked_notify'] = [
343 '#type' => 'checkbox',
344 '#title' => $this->t('Notify user when account is blocked'),
345 '#default_value' => $config->get('notify.status_blocked'),
347 $form['email_blocked']['settings'] = [
348 '#type' => 'container',
350 // Hide the additional settings when the blocked email is disabled.
352 'input[name="user_mail_status_blocked_notify"]' => ['checked' => FALSE],
356 $form['email_blocked']['settings']['user_mail_status_blocked_subject'] = [
357 '#type' => 'textfield',
358 '#title' => $this->t('Subject'),
359 '#default_value' => $mail_config->get('status_blocked.subject'),
362 $form['email_blocked']['settings']['user_mail_status_blocked_body'] = [
363 '#type' => 'textarea',
364 '#title' => $this->t('Body'),
365 '#default_value' => $mail_config->get('status_blocked.body'),
369 $form['email_cancel_confirm'] = [
370 '#type' => 'details',
371 '#title' => $this->t('Account cancellation confirmation'),
372 '#description' => $this->t('Edit the email messages sent to users when they attempt to cancel their accounts.') . ' ' . $email_token_help,
375 $form['email_cancel_confirm']['user_mail_cancel_confirm_subject'] = [
376 '#type' => 'textfield',
377 '#title' => $this->t('Subject'),
378 '#default_value' => $mail_config->get('cancel_confirm.subject'),
381 $form['email_cancel_confirm']['user_mail_cancel_confirm_body'] = [
382 '#type' => 'textarea',
383 '#title' => $this->t('Body'),
384 '#default_value' => $mail_config->get('cancel_confirm.body'),
388 $form['email_canceled'] = [
389 '#type' => 'details',
390 '#title' => $this->t('Account canceled'),
391 '#description' => $this->t('Enable and edit email messages sent to users when their accounts are canceled.') . ' ' . $email_token_help,
394 $form['email_canceled']['user_mail_status_canceled_notify'] = [
395 '#type' => 'checkbox',
396 '#title' => $this->t('Notify user when account is canceled'),
397 '#default_value' => $config->get('notify.status_canceled'),
399 $form['email_canceled']['settings'] = [
400 '#type' => 'container',
402 // Hide the settings when the cancel notify checkbox is disabled.
404 'input[name="user_mail_status_canceled_notify"]' => ['checked' => FALSE],
408 $form['email_canceled']['settings']['user_mail_status_canceled_subject'] = [
409 '#type' => 'textfield',
410 '#title' => $this->t('Subject'),
411 '#default_value' => $mail_config->get('status_canceled.subject'),
414 $form['email_canceled']['settings']['user_mail_status_canceled_body'] = [
415 '#type' => 'textarea',
416 '#title' => $this->t('Body'),
417 '#default_value' => $mail_config->get('status_canceled.body'),
427 public function submitForm(array &$form, FormStateInterface $form_state) {
428 parent::submitForm($form, $form_state);
430 $this->config('user.settings')
431 ->set('anonymous', $form_state->getValue('anonymous'))
432 ->set('register', $form_state->getValue('user_register'))
433 ->set('password_strength', $form_state->getValue('user_password_strength'))
434 ->set('verify_mail', $form_state->getValue('user_email_verification'))
435 ->set('cancel_method', $form_state->getValue('user_cancel_method'))
436 ->set('notify.status_activated', $form_state->getValue('user_mail_status_activated_notify'))
437 ->set('notify.status_blocked', $form_state->getValue('user_mail_status_blocked_notify'))
438 ->set('notify.status_canceled', $form_state->getValue('user_mail_status_canceled_notify'))
440 $this->config('user.mail')
441 ->set('cancel_confirm.body', $form_state->getValue('user_mail_cancel_confirm_body'))
442 ->set('cancel_confirm.subject', $form_state->getValue('user_mail_cancel_confirm_subject'))
443 ->set('password_reset.body', $form_state->getValue('user_mail_password_reset_body'))
444 ->set('password_reset.subject', $form_state->getValue('user_mail_password_reset_subject'))
445 ->set('register_admin_created.body', $form_state->getValue('user_mail_register_admin_created_body'))
446 ->set('register_admin_created.subject', $form_state->getValue('user_mail_register_admin_created_subject'))
447 ->set('register_no_approval_required.body', $form_state->getValue('user_mail_register_no_approval_required_body'))
448 ->set('register_no_approval_required.subject', $form_state->getValue('user_mail_register_no_approval_required_subject'))
449 ->set('register_pending_approval.body', $form_state->getValue('user_mail_register_pending_approval_body'))
450 ->set('register_pending_approval.subject', $form_state->getValue('user_mail_register_pending_approval_subject'))
451 ->set('register_pending_approval_admin.body', $form_state->getValue('register_pending_approval_admin_body'))
452 ->set('register_pending_approval_admin.subject', $form_state->getValue('register_pending_approval_admin_subject'))
453 ->set('status_activated.body', $form_state->getValue('user_mail_status_activated_body'))
454 ->set('status_activated.subject', $form_state->getValue('user_mail_status_activated_subject'))
455 ->set('status_blocked.body', $form_state->getValue('user_mail_status_blocked_body'))
456 ->set('status_blocked.subject', $form_state->getValue('user_mail_status_blocked_subject'))
457 ->set('status_canceled.body', $form_state->getValue('user_mail_status_canceled_body'))
458 ->set('status_canceled.subject', $form_state->getValue('user_mail_status_canceled_subject'))
460 $this->config('system.site')
461 ->set('mail_notification', $form_state->getValue('mail_notification_address'))
464 // Change the admin role.
465 if ($form_state->hasValue('user_admin_role')) {
466 $admin_roles = $this->roleStorage->getQuery()
467 ->condition('is_admin', TRUE)
470 foreach ($admin_roles as $rid) {
471 $this->roleStorage->load($rid)->setIsAdmin(FALSE)->save();
474 $new_admin_role = $form_state->getValue('user_admin_role');
475 if ($new_admin_role) {
476 $this->roleStorage->load($new_admin_role)->setIsAdmin(TRUE)->save();