You are here

class SettingsForm in Advanced Page Expiration 8

Hierarchy

Expanded class hierarchy of SettingsForm

1 string reference to 'SettingsForm'
ape.routing.yml in ./ape.routing.yml
ape.routing.yml

File

src/Form/SettingsForm.php, line 12

Namespace

Drupal\ape\Form
View source
class SettingsForm extends ConfigFormBase {

  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

  /**
   * Alternatives Condition
   *
   * @var \Drupal\system\Plugin\Condition\RequestPath
   */
  protected $alternatives;

  /**
   * Excluded Condition
   *
   * @var \Drupal\system\Plugin\Condition\RequestPath
   */
  protected $excluded;

  /**
   * Constructs a PerformanceForm object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   The date formatter service.
   * @param \Drupal\Component\Plugin\Factory\FactoryInterface $plugin_factory
   *   Factory for condition plugin manager.
   */
  public function __construct(ConfigFactoryInterface $config_factory, DateFormatterInterface $date_formatter, FactoryInterface $plugin_factory) {
    parent::__construct($config_factory);
    $this->dateFormatter = $date_formatter;
    $this->excluded = $plugin_factory
      ->createInstance('request_path');
    $this->alternatives = $plugin_factory
      ->createInstance('request_path');
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('date.formatter'), $container
      ->get('plugin.manager.condition'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'ape_settings';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'ape.settings',
      'system.performance',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['#tree'] = TRUE;
    $config_system = $this
      ->config('system.performance');
    $config_ape = $this
      ->config('ape.settings');
    $form['page_caching'] = array(
      '#type' => 'fieldset',
      '#title' => $this
        ->t('General page caching'),
      '#collapsible' => FALSE,
      '#collapsed' => FALSE,
    );
    $period = [
      0,
      60,
      180,
      300,
      600,
      900,
      1800,
      2700,
      3600,
      10800,
      21600,
      32400,
      43200,
      86400,
      604800,
      2592000,
      31536000,
    ];
    $period = array_map(array(
      $this->dateFormatter,
      'formatInterval',
    ), array_combine($period, $period));
    $period[0] = '<' . $this
      ->t('no caching') . '>';
    $form['page_caching']['page_cache_maximum_age'] = array(
      '#type' => 'select',
      '#title' => $this
        ->t('Global page expiration'),
      '#options' => $period,
      '#default_value' => $config_system
        ->get('cache.page.max_age'),
      '#description' => $this
        ->t('The standard expiration lifetime for cached pages. Ideally this is set as long as possible.'),
    );

    // Pages visibility plugin.
    $this->excluded
      ->setConfig('pages', $config_ape
      ->get('exclusions'));
    $form['page_caching'] += $this->excluded
      ->buildConfigurationForm([], $form_state);
    unset($form['page_caching']['negate']);
    $form['page_caching']['pages']['#title'] = $this
      ->t('Pages to exclude from caching');
    $form['page_caching_alternative'] = array(
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Alternative page caching'),
      '#collapsible' => FALSE,
      '#collapsed' => FALSE,
    );
    $form['page_caching_alternative']['ape_alternative_lifetime'] = array(
      '#type' => 'select',
      '#title' => $this
        ->t('Alternative page expiration'),
      '#options' => $period,
      '#default_value' => $config_ape
        ->get('lifetime.alternatives'),
      '#description' => $this
        ->t('An alternative page expiration lifetime. Useful for pages that should refresh at a different rate than most pages, such as a short interval like 5 minutes.'),
    );

    // Pages visibility plugin.
    $this->alternatives
      ->setConfig('pages', $config_ape
      ->get('alternatives'));
    $form['page_caching_alternative'] += $this->alternatives
      ->buildConfigurationForm([], $form_state);
    unset($form['page_caching_alternative']['negate']);
    $form['page_caching_alternative']['pages']['#title'] = $this
      ->t('Pages that should apply alternative cache length');
    $form['server_codes'] = array(
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Server response caching'),
      '#collapsible' => FALSE,
      '#collapsed' => FALSE,
    );
    $form['server_codes']['ape_301_lifetime'] = array(
      '#type' => 'select',
      '#title' => $this
        ->t('301 Redirects Expiration'),
      '#options' => $period,
      '#default_value' => $config_ape
        ->get('lifetime.301'),
      '#description' => $this
        ->t('Set a cache lifetime for all 301 redirects.'),
    );
    $form['server_codes']['ape_302_lifetime'] = array(
      '#type' => 'select',
      '#title' => $this
        ->t('302 Redirects Expiration'),
      '#options' => $period,
      '#default_value' => $config_ape
        ->get('lifetime.302'),
      '#description' => $this
        ->t('Set a cache lifetime for all 302 redirects.'),
    );
    $form['server_codes']['ape_404_lifetime'] = array(
      '#type' => 'select',
      '#title' => $this
        ->t('404 Page Not Found Expiration'),
      '#options' => $period,
      '#default_value' => $config_ape
        ->get('lifetime.404'),
      '#description' => $this
        ->t('Set a cache lifetime for all 404 Page Not Found responses.'),
    );
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->excluded
      ->setConfig('pages', $form_state
      ->getValue([
      'page_caching',
      'pages',
    ]));
    $this->alternatives
      ->setConfig('pages', $form_state
      ->getValue([
      'page_caching_alternative',
      'pages',
    ]));
    $this
      ->config('system.performance')
      ->set('cache.page.max_age', $form_state
      ->getValue([
      'page_caching',
      'page_cache_maximum_age',
    ]))
      ->save();
    $this
      ->config('ape.settings')
      ->set('alternatives', $this->alternatives
      ->getConfig()['pages'])
      ->set('exclusions', $this->excluded
      ->getConfig()['pages'])
      ->set('lifetime.alternatives', $form_state
      ->getValue([
      'page_caching_alternative',
      'ape_alternative_lifetime',
    ]))
      ->set('lifetime.301', $form_state
      ->getValue([
      'server_codes',
      'ape_301_lifetime',
    ]))
      ->set('lifetime.302', $form_state
      ->getValue([
      'server_codes',
      'ape_302_lifetime',
    ]))
      ->set('lifetime.404', $form_state
      ->getValue([
      'server_codes',
      'ape_404_lifetime',
    ]))
      ->save();
    parent::submitForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
SettingsForm::$alternatives protected property Alternatives Condition
SettingsForm::$dateFormatter protected property The date formatter service.
SettingsForm::$excluded protected property Excluded Condition
SettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
SettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
SettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
SettingsForm::__construct public function Constructs a PerformanceForm object. Overrides ConfigFormBase::__construct
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.