Yaffs site version 1.1
[yaffs-website] / web / modules / contrib / advagg / src / Form / SettingsForm.php
1 <?php
2
3 namespace Drupal\advagg\Form;
4
5 use Drupal\Core\Asset\AssetCollectionOptimizerInterface;
6 use Drupal\Core\Cache\Cache;
7 use Drupal\Core\Config\ConfigFactoryInterface;
8 use Drupal\Core\Datetime\DateFormatterInterface;
9 use Drupal\Core\Extension\ModuleHandlerInterface;
10 use Drupal\Core\Form\ConfigFormBase;
11 use Drupal\Core\Form\FormStateInterface;
12 use Drupal\Core\State\StateInterface;
13 use Drupal\Core\Url;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
15
16 /**
17  * Configure advagg settings for this site.
18  */
19 class SettingsForm extends ConfigFormBase {
20
21   /**
22    * The CSS asset collection optimizer service.
23    *
24    * @var \Drupal\Core\Asset\AssetCollectionOptimizerInterface
25    */
26   protected $cssCollectionOptimizer;
27
28   /**
29    * The JavaScript asset collection optimizer service.
30    *
31    * @var \Drupal\Core\Asset\AssetCollectionOptimizerInterface
32    */
33   protected $jsCollectionOptimizer;
34
35   /**
36    * The date formatter service.
37    *
38    * @var \Drupal\Core\Datetime\DateFormatterInterface
39    */
40   protected $dateFormatter;
41
42   /**
43    * The state service.
44    *
45    * @var \Drupal\Core\State\StateInterface
46    */
47   protected $state;
48
49   /**
50    * Module handler service.
51    *
52    * @var \Drupal\Core\Extension\ModuleHandlerInterface
53    */
54   protected $moduleHandler;
55
56   /**
57    * Constructs a SettingsForm object.
58    *
59    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
60    *   The factory for configuration objects.
61    * @param \Drupal\Core\Asset\AssetCollectionOptimizerInterface $css_collection_optimizer
62    *   The CSS asset collection optimizer service.
63    * @param \Drupal\Core\Asset\AssetCollectionOptimizerInterface $js_collection_optimizer
64    *   The JavaScript asset collection optimizer service.
65    * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
66    *   The Date formatter service.
67    * @param \Drupal\Core\State\StateInterface $state
68    *   The state service.
69    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
70    *   The module handler service.
71    */
72   public function __construct(ConfigFactoryInterface $config_factory, AssetCollectionOptimizerInterface $css_collection_optimizer, AssetCollectionOptimizerInterface $js_collection_optimizer, DateFormatterInterface $date_formatter, StateInterface $state, ModuleHandlerInterface $module_handler) {
73     parent::__construct($config_factory);
74
75     $this->cssCollectionOptimizer = $css_collection_optimizer;
76     $this->jsCollectionOptimizer = $js_collection_optimizer;
77     $this->dateFormatter = $date_formatter;
78     $this->state = $state;
79     $this->moduleHandler = $module_handler;
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   public static function create(ContainerInterface $container) {
86     return new static(
87       $container->get('config.factory'),
88       $container->get('asset.css.collection_optimizer'),
89       $container->get('asset.js.collection_optimizer'),
90       $container->get('date.formatter'),
91       $container->get('state'),
92       $container->get('module_handler')
93     );
94   }
95
96   /**
97    * {@inheritdoc}
98    */
99   public function getFormId() {
100     return 'advagg_settings';
101   }
102
103   /**
104    * {@inheritdoc}
105    */
106   protected function getEditableConfigNames() {
107     return ['advagg.settings', 'system.performance'];
108   }
109
110   /**
111    * {@inheritdoc}
112    */
113   public function buildForm(array $form, FormStateInterface $form_state) {
114     $config = $this->config('advagg.settings');
115     $form = [];
116     $form['global'] = [
117       '#type' => 'fieldset',
118       '#title' => $this->t('Global Options'),
119     ];
120     $form['global']['enabled'] = [
121       '#type' => 'checkbox',
122       '#title' => $this->t('Enable advanced aggregation'),
123       '#default_value' => $config->get('enabled'),
124       '#description' => $this->t('Uncheck this box to completely disable AdvAgg functionality.'),
125     ];
126     $form['global']['core_groups'] = [
127       '#type' => 'checkbox',
128       '#title' => $this->t('Use cores grouping logic'),
129       '#default_value' => $config->get('css.combine_media') || $config->get('css.ie.limit_selectors') ? FALSE : $config->get('core_groups'),
130       '#description' => $this->t('Will group files just like core does.'),
131       '#states' => [
132         'enabled' => [
133           '#edit-css-combine-media' => ['checked' => FALSE],
134           '#edit-css-ie-limit-selectors' => ['checked' => FALSE],
135         ],
136       ],
137     ];
138     $form['global']['dns_prefetch'] = [
139       '#type' => 'checkbox',
140       '#title' => $this->t('Use DNS Prefetch for external CSS/JS.'),
141       '#default_value' => $config->get('dns_prefetch'),
142       '#description' => $this->t('Start the DNS lookup for external CSS and JavaScript files as soon as possible.'),
143     ];
144     $options = [
145       -1 => $this->t('Development'),
146       1 => $this->t('Normal'),
147       3 => $this->t('High'),
148       5 => $this->t('Aggressive'),
149     ];
150
151     $form['global']['cache_level'] = [
152       '#type' => 'radios',
153       '#title' => $this->t('AdvAgg Cache Settings'),
154       '#default_value' => $config->get('cache_level'),
155       '#options' => $options,
156       '#description' => $this->t("No performance data yet but most use cases will probably want to use the Normal cache mode.", [
157         '@information' => Url::fromRoute('advagg.info')->toString(),
158       ]),
159     ];
160
161     $form['global']['dev_container'] = [
162       '#type' => 'container',
163       '#states' => [
164         'visible' => [
165           ':input[name="cache_level"]' => ['value' => '-1'],
166         ],
167       ],
168     ];
169
170     // Show msg about advagg css minify.
171     if ($this->moduleHandler->moduleExists('advagg_css_minify') && $this->config('advagg_css_minify.settings')->get('advagg_css_minifier') > 0) {
172       $form['global']['dev_container']['advagg_css_compress_msg'] = [
173         '#markup' => '<p>' . $this->t('The <a href="@css">AdvAgg CSS Minify module</a> is disabled when in development mode.', ['@css' => Url::fromRoute('advagg_css_minify.settings')->toString()]) . '</p>',
174       ];
175
176     }
177
178     // Show msg about advagg js minify.
179     if ($this->moduleHandler->moduleExists('advagg_js_minify') && $this->config('advagg_js_minify.settings')->get('advagg_js_minifier')) {
180       $form['global']['dev_container']['advagg_js_minify_msg'] = [
181         '#markup' => '<p>' . $this->t('The <a href="@js">AdvAgg JS Minify module</a> is disabled when in development mode.', ['@js' => Url::fromRoute('advagg_js_minify.settings')->toString()]) . '</p>',
182       ];
183     }
184
185     $form['global']['cron'] = [
186       '#type' => 'details',
187       '#title' => $this->t('Cron Options'),
188       '#description' => $this->t('Unless you have a good reason to adjust these values you should leave them alone.'),
189     ];
190
191     $short_times = [
192       900 => $this->t('15 minutes'),
193       1800 => $this->t('30 minutes'),
194       2700 => $this->t('45 minutes'),
195       3600 => $this->t('1 hour'),
196       7200 => $this->t('2 hours'),
197       14400 => $this->t('4 hours'),
198       21600 => $this->t('6 hours'),
199       43200 => $this->t('12 hours'),
200       64800 => $this->t('18 hours'),
201       86400 => $this->t('1 day'),
202       172800 => $this->t('2 days'),
203     ];
204
205     $long_times = [
206       172800 => $this->t('2 days'),
207       259200 => $this->t('3 days'),
208       345600 => $this->t('4 days'),
209       432000 => $this->t('5 days'),
210       518400 => $this->t('6 days'),
211       604800 => $this->t('1 week'),
212       1209600 => $this->t('2 week'),
213       1814400 => $this->t('3 week'),
214       2592000 => $this->t('1 month'),
215       3628800 => $this->t('6 weeks'),
216       4838400 => $this->t('2 months'),
217     ];
218     $last_ran = $this->state->get('advagg.cron_timestamp', NULL);
219     if ($last_ran) {
220       $last_ran = $this->t('@time ago', ['@time' => $this->dateFormatter->formatInterval(REQUEST_TIME - $last_ran)]);
221     }
222     else {
223       $last_ran = $this->t('never');
224     }
225     $form['global']['cron']['cron_frequency'] = [
226       '#type' => 'select',
227       '#options' => $short_times,
228       '#title' => 'Minimum amount of time between advagg_cron() runs.',
229       '#default_value' => $config->get('cron_frequency'),
230       '#description' => $this->t('The default value for this is %value. The last time advagg_cron was ran is %time.', [
231         '%value' => $this->dateFormatter->formatInterval($config->get('cron_frequency')),
232         '%time' => $last_ran,
233       ]),
234     ];
235
236     $form['global']['cron']['stale_file_threshold'] = [
237       '#type' => 'select',
238       '#options' => $long_times,
239       '#title' => 'Delete aggregates modified more than a set time ago.',
240       '#default_value' => $this->config('system.performance')->get('stale_file_threshold'),
241       '#description' => $this->t('The default value for this is %value.', [
242         '%value' => $this->dateFormatter->formatInterval($this->config('system.performance')->getOriginal('stale_file_threshold')),
243       ]),
244     ];
245
246     $form['global']['obscure'] = [
247       '#type' => 'details',
248       '#title' => $this->t('Obscure Options'),
249       '#description' => $this->t('Some of the more obscure AdvAgg settings. Odds are you do not need to change anything in here.'),
250     ];
251     $form['global']['obscure']['css_gzip'] = [
252       '#type' => 'checkbox',
253       '#title' => $this->t('Gzip CSS assets'),
254       '#default_value' => $this->config('system.performance')->get('css.gzip'),
255       '#description' => $this->t('This should be enabled unless you are experiencing corrupted compressed asset files.'),
256     ];
257     $form['global']['obscure']['js_gzip'] = [
258       '#type' => 'checkbox',
259       '#title' => $this->t('Gzip JavaScript assets'),
260       '#default_value' => $this->config('system.performance')->get('js.gzip'),
261       '#description' => $this->t('This should be enabled unless you are experiencing corrupted compressed asset files.'),
262     ];
263     $form['global']['obscure']['include_base_url'] = [
264       '#type' => 'checkbox',
265       '#title' => $this->t('Include the base_url variable in the hooks hash array.'),
266       '#default_value' => $config->get('include_base_url'),
267       '#description' => $this->t('If you would like a unique set of aggregates for every permutation of the base_url (current value: %value) then enable this setting. <a href="@issue">Read more</a>.', [
268         '%value' => $GLOBALS['base_url'],
269         '@issue' => 'https://www.drupal.org/node/2353811',
270       ]),
271     ];
272     $form['global']['obscure']['path_convert_absolute_to_protocol_relative'] = [
273       '#type' => 'checkbox',
274       '#title' => $this->t('Convert absolute paths to be protocol relative paths.'),
275       '#default_value' => $config->get('path.convert.absolute_to_protocol_relative'),
276       '#description' => $this->t('If the src to a CSS/JS file points starts with http:// or https://, convert it to use a protocol relative path //. Will also convert url() references inside of css files.'),
277       '#states' => [
278         'enabled' => [
279           '#edit-path-convert-force-https' => ['checked' => FALSE],
280         ],
281       ],
282     ];
283     $form['global']['obscure']['path_convert_force_https'] = [
284       '#type' => 'checkbox',
285       '#title' => $this->t('Convert http:// to https://.'),
286       '#default_value' => $config->get('path.convert.force_https'),
287       '#description' => $this->t('If the src to a CSS/JS file starts with http:// convert it https://. Will also convert url() references inside of css files.'),
288       '#states' => [
289         'enabled' => [
290           '#edit-path-convert-absolut-to-protocol-relative' => ['checked' => FALSE],
291         ],
292       ],
293     ];
294
295     $form['css'] = [
296       '#type' => 'details',
297       '#title' => $this->t('CSS Options'),
298       '#open' => TRUE,
299     ];
300     $form['css']['css_combine_media'] = [
301       '#type' => 'checkbox',
302       '#title' => $this->t('Combine CSS files by using media queries'),
303       '#default_value' => $config->get('css.combine_media'),
304       '#description' => $this->t('Will combine more CSS files together because different CSS media types can be used in the same file by using media queries. Use cores grouping logic needs to be unchecked in order for this to work. Also noted is that due to an issue with IE9, compatibility mode is forced off if this is enabled.'),
305       '#states' => [
306         'disabled' => [
307           '#edit-core-groups' => ['checked' => TRUE],
308         ],
309       ],
310     ];
311     $form['css']['css_ie_limit_selectors'] = [
312       '#type' => 'checkbox',
313       '#title' => $this->t('Prevent more than %limit CSS selectors in an aggregated CSS file', ['%limit' => $config->get('css.ie.selector_limit')]),
314       '#default_value' => $config->get('css.ie.limit_selectors'),
315       '#description' => $this->t('Internet Explorer before version 10; IE9, IE8, IE7, and IE6 all have 4095 as the limit for the maximum number of css selectors that can be in a file. Enabling this will prevent CSS aggregates from being created that exceed this limit. <a href="@link">More info</a>. Use cores grouping logic needs to be unchecked in order for this to work.', ['@link' => 'http://blogs.msdn.com/b/ieinternals/archive/2011/05/14/10164546.aspx']),
316       '#states' => [
317         'disabled' => [
318           '#edit-core-groups' => ['checked' => TRUE],
319         ],
320       ],
321     ];
322     $form['css']['css_ie_selector_limit'] = [
323       '#type' => 'textfield',
324       '#title' => $this->t('The selector count the IE CSS limiter should use'),
325       '#default_value' => $config->get('css.ie.selector_limit'),
326       '#description' => $this->t('Internet Explorer before version 10; IE9, IE8, IE7, and IE6 all have 4095 as the limit for the maximum number of css selectors that can be in a file. Use this field to modify the value used; 4095 sometimes may be still be too many with media queries.'),
327       '#states' => [
328         'visible' => [
329           '#edit-css-ie-limit-selectors' => ['checked' => TRUE],
330         ],
331         'disabled' => [
332           '#edit-css-ie-limit-selectors' => ['checked' => FALSE],
333         ],
334       ],
335     ];
336     $form['css']['css_fix_type'] = [
337       '#type' => 'checkbox',
338       '#title' => $this->t('Fix improperly set type'),
339       '#default_value' => $config->get('css.fix_type'),
340       '#description' => $this->t('If type is external but does not start with http, https, or // change it to be type file. If type is file but it starts with http, https, or // change type to be external. Note that if this is causing issues, odds are you have a double slash when there should be a single; see <a href="@link">this issue</a>', [
341         '@link' => 'https://www.drupal.org/node/2336217',
342       ]),
343     ];
344
345     $form['js'] = [
346       '#type' => 'details',
347       '#title' => $this->t('JS Options'),
348       '#open' => TRUE,
349     ];
350     $form['js']['js_fix_type'] = [
351       '#type' => 'checkbox',
352       '#title' => $this->t('Fix improperly set type'),
353       '#default_value' => $config->get('js_fix_type'),
354       '#description' => $this->t('If type is external but does not start with http, https, or // change it to be type file. If type is file but it starts with http, https, or // change type to be external. Note that if this is causing issues, odds are you have a double slash when there should be a single; see <a href="@link">this issue</a>', [
355         '@link' => 'https://www.drupal.org/node/2336217',
356       ]),
357     ];
358     $form['js']['js_preserve_external'] = [
359       '#type' => 'checkbox',
360       '#title' => $this->t('Do not change external to file if on same host.'),
361       '#default_value' => $config->get('js_preserve_external'),
362       '#description' => $this->t('If a JS file is set as external and is on the same hosts do not convert to file.'),
363       '#states' => [
364         'disabled' => [
365           '#edit-js-fix-type' => ['checked' => FALSE],
366         ],
367       ],
368     ];
369     return parent::buildForm($form, $form_state);
370   }
371
372   /**
373    * {@inheritdoc}
374    */
375   public function submitForm(array &$form, FormStateInterface $form_state) {
376     $this->config('advagg.settings')
377       ->set('css.fix_type', $form_state->getValue('css_fix_type'))
378       ->set('css.ie.limit_selectors', $form_state->getValue('css_ie_limit_selectors'))
379       ->set('css.ie.selector_limit', $form_state->getValue('css_ie_selector_limit'))
380       ->set('css.combine_media', $form_state->getValue('css_combine_media'))
381       ->set('path.convert.force_https', $form_state->getValue('path_convert_force_https'))
382       ->set('path.convert.absolute_to_protocol_relative', $form_state->getValue('path_convert_absolute_to_protocol_relative'))
383       ->set('enabled', $form_state->getValue('enabled'))
384       ->set('core_groups', $form_state->getValue('core_groups'))
385       ->set('dns_prefetch', $form_state->getValue('dns_prefetch'))
386       ->set('cache_level', $form_state->getValue('cache_level'))
387       ->set('cron_frequency', $form_state->getValue('cron_frequency'))
388       ->set('include_base_url', $form_state->getValue('include_base_url'))
389       ->set('js_fix_type', $form_state->getValue('js_fix_type'))
390       ->set('js_preserve_external', $form_state->getValue('js_preserve_external'))
391       ->save();
392     $this->config('system.performance')
393       ->set('stale_file_threshold', $form_state->getValue('stale_file_threshold'))
394       ->set('css.gzip', $form_state->getValue('css_gzip'))
395       ->set('js.gzip', $form_state->getValue('js_gzip'))
396       ->save();
397
398     // Clear relevant caches.
399     $this->cssCollectionOptimizer->deleteAll();
400     $this->jsCollectionOptimizer->deleteAll();
401     Cache::invalidateTags(['library_info', 'advagg_css', 'advagg_js']);
402
403     parent::submitForm($form, $form_state);
404   }
405
406 }