SiteMaintenanceModeForm.php 3.3 KB

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