58b1f1a9d62c1c36236f7702b3f5c475f29982c3
[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    * {@inheritdoc}
61    */
62   public function getFormId() {
63     return 'system_site_maintenance_mode';
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   protected function getEditableConfigNames() {
70     return ['system.maintenance'];
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function buildForm(array $form, FormStateInterface $form_state) {
77     $config = $this->config('system.maintenance');
78     $permissions = $this->permissionHandler->getPermissions();
79     $permission_label = $permissions['access site in maintenance mode']['title'];
80     $form['maintenance_mode'] = [
81       '#type' => 'checkbox',
82       '#title' => t('Put site into maintenance mode'),
83       '#default_value' => $this->state->get('system.maintenance_mode'),
84       '#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')]),
85     ];
86     $form['maintenance_mode_message'] = [
87       '#type' => 'textarea',
88       '#title' => t('Message to display when in maintenance mode'),
89       '#default_value' => $config->get('message'),
90     ];
91
92     return parent::buildForm($form, $form_state);
93   }
94
95   /**
96    * {@inheritdoc}
97    */
98   public function submitForm(array &$form, FormStateInterface $form_state) {
99     $this->config('system.maintenance')
100       ->set('message', $form_state->getValue('maintenance_mode_message'))
101       ->save();
102
103     $this->state->set('system.maintenance_mode', $form_state->getValue('maintenance_mode'));
104     parent::submitForm($form, $form_state);
105   }
106
107 }