dae500df48403e373861ccccd9cd3cb8eb76b0c7
[yaffs-website] / web / core / modules / system / src / Form / SiteMaintenanceModeForm.php
1 <?php
2
3 namespace Drupal\system\Form;
4
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\State\StateInterface;
8 use Drupal\Core\Form\ConfigFormBase;
9 use Drupal\user\PermissionHandlerInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Configure maintenance settings for this site.
14  *
15  * @internal
16  */
17 class SiteMaintenanceModeForm extends ConfigFormBase {
18
19   /**
20    * The state keyvalue collection.
21    *
22    * @var \Drupal\Core\State\StateInterface
23    */
24   protected $state;
25
26   /**
27   * The permission handler.
28   *
29   * @var \Drupal\user\PermissionHandlerInterface
30   */
31   protected $permissionHandler;
32
33   /**
34    * Constructs a new SiteMaintenanceModeForm.
35    *
36    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
37    *   The factory for configuration objects.
38    * @param \Drupal\Core\State\StateInterface $state
39    *   The state keyvalue collection to use.
40    * @param \Drupal\user\PermissionHandlerInterface $permission_handler
41    *   The permission handler.
42    */
43   public function __construct(ConfigFactoryInterface $config_factory, StateInterface $state, PermissionHandlerInterface $permission_handler) {
44     parent::__construct($config_factory);
45     $this->state = $state;
46     $this->permissionHandler = $permission_handler;
47   }
48
49   /**
50    * {@inheritdoc}
51    */
52   public static function create(ContainerInterface $container) {
53     return new static(
54       $container->get('config.factory'),
55       $container->get('state'),
56       $container->get('user.permissions')
57     );
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public function getFormId() {
64     return 'system_site_maintenance_mode';
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   protected function getEditableConfigNames() {
71     return ['system.maintenance'];
72   }
73
74   /**
75    * {@inheritdoc}
76    */
77   public function buildForm(array $form, FormStateInterface $form_state) {
78     $config = $this->config('system.maintenance');
79     $permissions = $this->permissionHandler->getPermissions();
80     $permission_label = $permissions['access site in maintenance mode']['title'];
81     $form['maintenance_mode'] = [
82       '#type' => 'checkbox',
83       '#title' => t('Put site into maintenance mode'),
84       '#default_value' => $this->state->get('system.maintenance_mode'),
85       '#description' => t('Visitors will only see the maintenance mode message. Only users with the "@permission-label" <a href=":permissions-url">permission</a> will be able to access the site. Authorized users can log in directly via the <a href=":user-login">user login</a> page.', ['@permission-label' => $permission_label, ':permissions-url' => $this->url('user.admin_permissions'), ':user-login' => $this->url('user.login')]),
86     ];
87     $form['maintenance_mode_message'] = [
88       '#type' => 'textarea',
89       '#title' => t('Message to display when in maintenance mode'),
90       '#default_value' => $config->get('message'),
91     ];
92
93     return parent::buildForm($form, $form_state);
94   }
95
96   /**
97    * {@inheritdoc}
98    */
99   public function submitForm(array &$form, FormStateInterface $form_state) {
100     $this->config('system.maintenance')
101       ->set('message', $form_state->getValue('maintenance_mode_message'))
102       ->save();
103
104     $this->state->set('system.maintenance_mode', $form_state->getValue('maintenance_mode'));
105     parent::submitForm($form, $form_state);
106   }
107
108 }