Upgraded drupal core with security updates
[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 class SiteMaintenanceModeForm extends ConfigFormBase {
16
17   /**
18    * The state keyvalue collection.
19    *
20    * @var \Drupal\Core\State\StateInterface
21    */
22   protected $state;
23
24   /**
25   * The permission handler.
26   *
27   * @var \Drupal\user\PermissionHandlerInterface
28   */
29   protected $permissionHandler;
30
31   /**
32    * Constructs a new SiteMaintenanceModeForm.
33    *
34    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
35    *   The factory for configuration objects.
36    * @param \Drupal\Core\State\StateInterface $state
37    *   The state keyvalue collection to use.
38    * @param \Drupal\user\PermissionHandlerInterface $permission_handler
39    *   The permission handler.
40    */
41   public function __construct(ConfigFactoryInterface $config_factory, StateInterface $state, PermissionHandlerInterface $permission_handler) {
42     parent::__construct($config_factory);
43     $this->state = $state;
44     $this->permissionHandler = $permission_handler;
45   }
46
47   /**
48    * {@inheritdoc}
49    */
50   public static function create(ContainerInterface $container) {
51     return new static(
52       $container->get('config.factory'),
53       $container->get('state'),
54       $container->get('user.permissions')
55     );
56   }
57   /**
58    * {@inheritdoc}
59    */
60   public function getFormId() {
61     return 'system_site_maintenance_mode';
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   protected function getEditableConfigNames() {
68     return ['system.maintenance'];
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function buildForm(array $form, FormStateInterface $form_state) {
75     $config = $this->config('system.maintenance');
76     $permissions = $this->permissionHandler->getPermissions();
77     $permission_label = $permissions['access site in maintenance mode']['title'];
78     $form['maintenance_mode'] = [
79       '#type' => 'checkbox',
80       '#title' => t('Put site into maintenance mode'),
81       '#default_value' => $this->state->get('system.maintenance_mode'),
82       '#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')]),
83     ];
84     $form['maintenance_mode_message'] = [
85       '#type' => 'textarea',
86       '#title' => t('Message to display when in maintenance mode'),
87       '#default_value' => $config->get('message'),
88     ];
89
90     return parent::buildForm($form, $form_state);
91   }
92
93   /**
94    * {@inheritdoc}
95    */
96   public function submitForm(array &$form, FormStateInterface $form_state) {
97     $this->config('system.maintenance')
98       ->set('message', $form_state->getValue('maintenance_mode_message'))
99       ->save();
100
101     $this->state->set('system.maintenance_mode', $form_state->getValue('maintenance_mode'));
102     parent::submitForm($form, $form_state);
103   }
104
105 }