"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/modules/system/src/Form/SiteMaintenanceModeForm.php" (26 Nov 2020, 3428 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "SiteMaintenanceModeForm.php" see the Fossies "Dox" file reference documentation.

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