PerformanceForm.php 5.85 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\system\Form;

5
use Drupal\Core\Asset\AssetCollectionOptimizerInterface;
6
use Drupal\Core\Form\ConfigFormBase;
7
use Drupal\Core\Config\ConfigFactoryInterface;
8
use Drupal\Core\Datetime\DateFormatterInterface;
9
use Drupal\Core\Form\FormStateInterface;
10 11 12 13 14
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Configure performance settings for this site.
 */
15
class PerformanceForm extends ConfigFormBase {
16

17 18 19
  /**
   * The date formatter service.
   *
20
   * @var \Drupal\Core\Datetime\DateFormatterInterface
21 22 23
   */
  protected $dateFormatter;

24 25 26 27 28 29 30 31 32 33 34 35 36 37
  /**
   * The CSS asset collection optimizer service.
   *
   * @var \Drupal\Core\Asset\AssetCollectionOptimizerInterface
   */
  protected $cssCollectionOptimizer;

  /**
   * The JavaScript asset collection optimizer service.
   *
   * @var \Drupal\Core\Asset\AssetCollectionOptimizerInterface
   */
  protected $jsCollectionOptimizer;

38 39 40
  /**
   * Constructs a PerformanceForm object.
   *
41
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
42
   *   The factory for configuration objects.
43
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
44
   *   The date formatter service.
45 46 47 48
   * @param \Drupal\Core\Asset\AssetCollectionOptimizerInterface $css_collection_optimizer
   *   The CSS asset collection optimizer service.
   * @param \Drupal\Core\Asset\AssetCollectionOptimizerInterface $js_collection_optimizer
   *   The JavaScript asset collection optimizer service.
49
   */
50
  public function __construct(ConfigFactoryInterface $config_factory, DateFormatterInterface $date_formatter, AssetCollectionOptimizerInterface $css_collection_optimizer, AssetCollectionOptimizerInterface $js_collection_optimizer) {
51
    parent::__construct($config_factory);
52

53
    $this->dateFormatter = $date_formatter;
54 55
    $this->cssCollectionOptimizer = $css_collection_optimizer;
    $this->jsCollectionOptimizer = $js_collection_optimizer;
56 57 58 59 60 61 62 63
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('config.factory'),
64 65 66
      $container->get('date.formatter'),
      $container->get('asset.css.collection_optimizer'),
      $container->get('asset.js.collection_optimizer')
67 68 69 70 71 72
    );
  }

  /**
   * {@inheritdoc}
   */
73
  public function getFormId() {
74
    return 'system_performance_settings';
75 76
  }

77 78 79 80 81 82 83
  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return ['system.performance'];
  }

84 85 86
  /**
   * {@inheritdoc}
   */
87
  public function buildForm(array $form, FormStateInterface $form_state) {
88
    $form['#attached']['library'][] = 'system/drupal.system';
89

90
    $config = $this->config('system.performance');
91

92
    $form['clear_cache'] = [
93 94
      '#type' => 'details',
      '#title' => t('Clear cache'),
95
      '#open' => TRUE,
96
    ];
97

98
    $form['clear_cache']['clear'] = [
99 100
      '#type' => 'submit',
      '#value' => t('Clear all caches'),
101 102
      '#submit' => ['::submitCacheClear'],
    ];
103

104
    $form['caching'] = [
105 106
      '#type' => 'details',
      '#title' => t('Caching'),
107
      '#open' => TRUE,
108
      '#description' => $this->t('Note: Drupal provides an internal page cache module that is recommended for small to medium-sized websites.'),
109
    ];
110
    // Identical options to the ones for block caching.
111
    // @see \Drupal\Core\Block\BlockBase::buildConfigurationForm()
112 113
    $period = [0, 60, 180, 300, 600, 900, 1800, 2700, 3600, 10800, 21600, 32400, 43200, 86400];
    $period = array_map([$this->dateFormatter, 'formatInterval'], array_combine($period, $period));
114
    $period[0] = '<' . t('no caching') . '>';
115
    $form['caching']['page_cache_maximum_age'] = [
116 117 118 119
      '#type' => 'select',
      '#title' => t('Page cache maximum age'),
      '#default_value' => $config->get('cache.page.max_age'),
      '#options' => $period,
120
      '#description' => t('The maximum time a page can be cached by browsers and proxies. This is used as the value for max-age in Cache-Control headers.'),
121
    ];
122 123 124 125 126 127

    $directory = 'public://';
    $is_writable = is_dir($directory) && is_writable($directory);
    $disabled = !$is_writable;
    $disabled_message = '';
    if (!$is_writable) {
128
      $disabled_message = ' ' . t('<strong class="error">Set up the <a href=":file-system">public files directory</a> to make these optimizations available.</strong>', [':file-system' => $this->url('system.file_system_settings')]);
129 130
    }

131
    $form['bandwidth_optimization'] = [
132 133
      '#type' => 'details',
      '#title' => t('Bandwidth optimization'),
134
      '#open' => TRUE,
135
      '#description' => t('External resources can be optimized automatically, which can reduce both the size and number of requests made to your website.') . $disabled_message,
136
    ];
137

138
    $form['bandwidth_optimization']['preprocess_css'] = [
139
      '#type' => 'checkbox',
140
      '#title' => t('Aggregate CSS files'),
141 142
      '#default_value' => $config->get('css.preprocess'),
      '#disabled' => $disabled,
143 144
    ];
    $form['bandwidth_optimization']['preprocess_js'] = [
145
      '#type' => 'checkbox',
146
      '#title' => t('Aggregate JavaScript files'),
147 148
      '#default_value' => $config->get('js.preprocess'),
      '#disabled' => $disabled,
149
    ];
150 151 152 153 154 155 156

    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
157
  public function submitForm(array &$form, FormStateInterface $form_state) {
158 159
    $this->cssCollectionOptimizer->deleteAll();
    $this->jsCollectionOptimizer->deleteAll();
160

161
    $this->config('system.performance')
162 163 164
      ->set('cache.page.max_age', $form_state->getValue('page_cache_maximum_age'))
      ->set('css.preprocess', $form_state->getValue('preprocess_css'))
      ->set('js.preprocess', $form_state->getValue('preprocess_js'))
165 166 167 168 169 170 171 172
      ->save();

    parent::submitForm($form, $form_state);
  }

  /**
   * Clears the caches.
   */
173
  public function submitCacheClear(array &$form, FormStateInterface $form_state) {
174 175 176 177 178
    drupal_flush_all_caches();
    drupal_set_message(t('Caches cleared.'));
  }

}