3 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 use Symfony\Component\DependencyInjection\ContainerInterface;
13 * Configure performance settings for this site.
15 class PerformanceForm extends ConfigFormBase {
18 * The date formatter service.
20 * @var \Drupal\Core\Datetime\DateFormatterInterface
22 protected $dateFormatter;
25 * The CSS asset collection optimizer service.
27 * @var \Drupal\Core\Asset\AssetCollectionOptimizerInterface
29 protected $cssCollectionOptimizer;
32 * The JavaScript asset collection optimizer service.
34 * @var \Drupal\Core\Asset\AssetCollectionOptimizerInterface
36 protected $jsCollectionOptimizer;
39 * 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 * @param \Drupal\Core\Asset\AssetCollectionOptimizerInterface $css_collection_optimizer
46 * The CSS asset collection optimizer service.
47 * @param \Drupal\Core\Asset\AssetCollectionOptimizerInterface $js_collection_optimizer
48 * The JavaScript asset collection optimizer service.
50 public function __construct(ConfigFactoryInterface $config_factory, DateFormatterInterface $date_formatter, AssetCollectionOptimizerInterface $css_collection_optimizer, AssetCollectionOptimizerInterface $js_collection_optimizer) {
51 parent::__construct($config_factory);
53 $this->dateFormatter = $date_formatter;
54 $this->cssCollectionOptimizer = $css_collection_optimizer;
55 $this->jsCollectionOptimizer = $js_collection_optimizer;
61 public static function create(ContainerInterface $container) {
63 $container->get('config.factory'),
64 $container->get('date.formatter'),
65 $container->get('asset.css.collection_optimizer'),
66 $container->get('asset.js.collection_optimizer')
73 public function getFormId() {
74 return 'system_performance_settings';
80 protected function getEditableConfigNames() {
81 return ['system.performance'];
87 public function buildForm(array $form, FormStateInterface $form_state) {
88 $form['#attached']['library'][] = 'system/drupal.system';
90 $config = $this->config('system.performance');
92 $form['clear_cache'] = [
94 '#title' => t('Clear cache'),
98 $form['clear_cache']['clear'] = [
100 '#value' => t('Clear all caches'),
101 '#submit' => ['::submitCacheClear'],
105 '#type' => 'details',
106 '#title' => t('Caching'),
108 '#description' => $this->t('Note: Drupal provides an internal page cache module that is recommended for small to medium-sized websites.'),
110 // Identical options to the ones for block caching.
111 // @see \Drupal\Core\Block\BlockBase::buildConfigurationForm()
112 $period = [0, 60, 180, 300, 600, 900, 1800, 2700, 3600, 10800, 21600, 32400, 43200, 86400];
113 $period = array_map([$this->dateFormatter, 'formatInterval'], array_combine($period, $period));
114 $period[0] = '<' . t('no caching') . '>';
115 $form['caching']['page_cache_maximum_age'] = [
117 '#title' => t('Page cache maximum age'),
118 '#default_value' => $config->get('cache.page.max_age'),
119 '#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.'),
123 $directory = 'public://';
124 $is_writable = is_dir($directory) && is_writable($directory);
125 $disabled = !$is_writable;
126 $disabled_message = '';
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')]);
131 $form['bandwidth_optimization'] = [
132 '#type' => 'details',
133 '#title' => t('Bandwidth optimization'),
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,
138 $form['bandwidth_optimization']['preprocess_css'] = [
139 '#type' => 'checkbox',
140 '#title' => t('Aggregate CSS files'),
141 '#default_value' => $config->get('css.preprocess'),
142 '#disabled' => $disabled,
144 $form['bandwidth_optimization']['preprocess_js'] = [
145 '#type' => 'checkbox',
146 '#title' => t('Aggregate JavaScript files'),
147 '#default_value' => $config->get('js.preprocess'),
148 '#disabled' => $disabled,
151 return parent::buildForm($form, $form_state);
157 public function submitForm(array &$form, FormStateInterface $form_state) {
158 $this->cssCollectionOptimizer->deleteAll();
159 $this->jsCollectionOptimizer->deleteAll();
161 $this->config('system.performance')
162 ->set('cache.page.max_age', $form_state->getValue('page_cache_maximum_age'))
163 ->set('css.preprocess', $form_state->getValue('preprocess_css'))
164 ->set('js.preprocess', $form_state->getValue('preprocess_js'))
167 parent::submitForm($form, $form_state);
173 public function submitCacheClear(array &$form, FormStateInterface $form_state) {
174 drupal_flush_all_caches();
175 drupal_set_message(t('Caches cleared.'));