Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / system / src / Form / PerformanceForm.php
1 <?php
2
3 namespace Drupal\system\Form;
4
5 use Drupal\Core\Asset\AssetCollectionOptimizerInterface;
6 use Drupal\Core\Extension\ModuleHandlerInterface;
7 use Drupal\Core\Form\ConfigFormBase;
8 use Drupal\Core\Config\ConfigFactoryInterface;
9 use Drupal\Core\Datetime\DateFormatterInterface;
10 use Drupal\Core\Form\FormStateInterface;
11 use Drupal\Core\Url;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13
14 /**
15  * Configure performance settings for this site.
16  *
17  * @internal
18  */
19 class PerformanceForm extends ConfigFormBase {
20
21   /**
22    * The date formatter service.
23    *
24    * @var \Drupal\Core\Datetime\DateFormatterInterface
25    */
26   protected $dateFormatter;
27
28   /**
29    * The CSS asset collection optimizer service.
30    *
31    * @var \Drupal\Core\Asset\AssetCollectionOptimizerInterface
32    */
33   protected $cssCollectionOptimizer;
34
35   /**
36    * The JavaScript asset collection optimizer service.
37    *
38    * @var \Drupal\Core\Asset\AssetCollectionOptimizerInterface
39    */
40   protected $jsCollectionOptimizer;
41
42   /**
43    * The module handler.
44    *
45    * @var \Drupal\Core\Extension\ModuleHandlerInterface
46    */
47   protected $moduleHandler;
48
49   /**
50    * Constructs a PerformanceForm object.
51    *
52    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
53    *   The factory for configuration objects.
54    * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
55    *   The date formatter service.
56    * @param \Drupal\Core\Asset\AssetCollectionOptimizerInterface $css_collection_optimizer
57    *   The CSS asset collection optimizer service.
58    * @param \Drupal\Core\Asset\AssetCollectionOptimizerInterface $js_collection_optimizer
59    *   The JavaScript asset collection optimizer service.
60    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
61    *   The module handler.
62    */
63   public function __construct(ConfigFactoryInterface $config_factory, DateFormatterInterface $date_formatter, AssetCollectionOptimizerInterface $css_collection_optimizer, AssetCollectionOptimizerInterface $js_collection_optimizer, ModuleHandlerInterface $module_handler) {
64     parent::__construct($config_factory);
65
66     $this->dateFormatter = $date_formatter;
67     $this->cssCollectionOptimizer = $css_collection_optimizer;
68     $this->jsCollectionOptimizer = $js_collection_optimizer;
69     $this->moduleHandler = $module_handler;
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   public static function create(ContainerInterface $container) {
76     return new static(
77       $container->get('config.factory'),
78       $container->get('date.formatter'),
79       $container->get('asset.css.collection_optimizer'),
80       $container->get('asset.js.collection_optimizer'),
81       $container->get('module_handler')
82     );
83   }
84
85   /**
86    * {@inheritdoc}
87    */
88   public function getFormId() {
89     return 'system_performance_settings';
90   }
91
92   /**
93    * {@inheritdoc}
94    */
95   protected function getEditableConfigNames() {
96     return ['system.performance'];
97   }
98
99   /**
100    * {@inheritdoc}
101    */
102   public function buildForm(array $form, FormStateInterface $form_state) {
103     $form['#attached']['library'][] = 'system/drupal.system';
104
105     $config = $this->config('system.performance');
106
107     $form['clear_cache'] = [
108       '#type' => 'details',
109       '#title' => t('Clear cache'),
110       '#open' => TRUE,
111     ];
112
113     $form['clear_cache']['clear'] = [
114       '#type' => 'submit',
115       '#value' => t('Clear all caches'),
116       '#submit' => ['::submitCacheClear'],
117     ];
118
119     $form['caching'] = [
120       '#type' => 'details',
121       '#title' => t('Caching'),
122       '#open' => TRUE,
123     ];
124     // Identical options to the ones for block caching.
125     // @see \Drupal\Core\Block\BlockBase::buildConfigurationForm()
126     $period = [0, 60, 180, 300, 600, 900, 1800, 2700, 3600, 10800, 21600, 32400, 43200, 86400];
127     $period = array_map([$this->dateFormatter, 'formatInterval'], array_combine($period, $period));
128     $period[0] = '<' . t('no caching') . '>';
129     $form['caching']['page_cache_maximum_age'] = [
130       '#type' => 'select',
131       '#title' => t('Browser and proxy cache maximum age'),
132       '#default_value' => $config->get('cache.page.max_age'),
133       '#options' => $period,
134       '#description' => t('This is used as the value for max-age in Cache-Control headers.'),
135     ];
136     $form['caching']['internal_page_cache'] = [
137       '#markup' => $this->t('Drupal provides an <a href=":module_enable">Internal Page Cache module</a> that is recommended for small to medium-sized websites.', [':module_enable' => Url::fromRoute('system.modules_list')->toString()]),
138       '#access' => !$this->moduleHandler->moduleExists('page_cache'),
139     ];
140
141     $directory = 'public://';
142     $is_writable = is_dir($directory) && is_writable($directory);
143     $disabled = !$is_writable;
144     $disabled_message = '';
145     if (!$is_writable) {
146       $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')]);
147     }
148
149     $form['bandwidth_optimization'] = [
150       '#type' => 'details',
151       '#title' => t('Bandwidth optimization'),
152       '#open' => TRUE,
153       '#description' => t('External resources can be optimized automatically, which can reduce both the size and number of requests made to your website.') . $disabled_message,
154     ];
155
156     $form['bandwidth_optimization']['preprocess_css'] = [
157       '#type' => 'checkbox',
158       '#title' => t('Aggregate CSS files'),
159       '#default_value' => $config->get('css.preprocess'),
160       '#disabled' => $disabled,
161     ];
162     $form['bandwidth_optimization']['preprocess_js'] = [
163       '#type' => 'checkbox',
164       '#title' => t('Aggregate JavaScript files'),
165       '#default_value' => $config->get('js.preprocess'),
166       '#disabled' => $disabled,
167     ];
168
169     return parent::buildForm($form, $form_state);
170   }
171
172   /**
173    * {@inheritdoc}
174    */
175   public function submitForm(array &$form, FormStateInterface $form_state) {
176     $this->cssCollectionOptimizer->deleteAll();
177     $this->jsCollectionOptimizer->deleteAll();
178
179     $this->config('system.performance')
180       ->set('cache.page.max_age', $form_state->getValue('page_cache_maximum_age'))
181       ->set('css.preprocess', $form_state->getValue('preprocess_css'))
182       ->set('js.preprocess', $form_state->getValue('preprocess_js'))
183       ->save();
184
185     parent::submitForm($form, $form_state);
186   }
187
188   /**
189    * Clears the caches.
190    */
191   public function submitCacheClear(array &$form, FormStateInterface $form_state) {
192     drupal_flush_all_caches();
193     $this->messenger()->addStatus($this->t('Caches cleared.'));
194   }
195
196 }