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\Cache\CacheBackendInterface;
9 use Drupal\Core\Datetime\DateFormatterInterface;
10 use Drupal\Core\Form\FormStateInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Configure performance settings for this site.
16 class PerformanceForm extends ConfigFormBase {
19 * The render cache bin.
21 * @var \Drupal\Core\Cache\CacheBackendInterface
23 protected $renderCache;
26 * The date formatter service.
28 * @var \Drupal\Core\Datetime\DateFormatterInterface
30 protected $dateFormatter;
33 * The CSS asset collection optimizer service.
35 * @var \Drupal\Core\Asset\AssetCollectionOptimizerInterface
37 protected $cssCollectionOptimizer;
40 * The JavaScript asset collection optimizer service.
42 * @var \Drupal\Core\Asset\AssetCollectionOptimizerInterface
44 protected $jsCollectionOptimizer;
47 * Constructs a PerformanceForm object.
49 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
50 * The factory for configuration objects.
51 * @param \Drupal\Core\Cache\CacheBackendInterface $render_cache
52 * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
53 * The date formatter service.
54 * @param \Drupal\Core\Asset\AssetCollectionOptimizerInterface $css_collection_optimizer
55 * The CSS asset collection optimizer service.
56 * @param \Drupal\Core\Asset\AssetCollectionOptimizerInterface $js_collection_optimizer
57 * The JavaScript asset collection optimizer service.
59 public function __construct(ConfigFactoryInterface $config_factory, CacheBackendInterface $render_cache, DateFormatterInterface $date_formatter, AssetCollectionOptimizerInterface $css_collection_optimizer, AssetCollectionOptimizerInterface $js_collection_optimizer) {
60 parent::__construct($config_factory);
62 $this->renderCache = $render_cache;
63 $this->dateFormatter = $date_formatter;
64 $this->cssCollectionOptimizer = $css_collection_optimizer;
65 $this->jsCollectionOptimizer = $js_collection_optimizer;
71 public static function create(ContainerInterface $container) {
73 $container->get('config.factory'),
74 $container->get('cache.render'),
75 $container->get('date.formatter'),
76 $container->get('asset.css.collection_optimizer'),
77 $container->get('asset.js.collection_optimizer')
84 public function getFormId() {
85 return 'system_performance_settings';
91 protected function getEditableConfigNames() {
92 return ['system.performance'];
98 public function buildForm(array $form, FormStateInterface $form_state) {
99 $form['#attached']['library'][] = 'system/drupal.system';
101 $config = $this->config('system.performance');
103 $form['clear_cache'] = [
104 '#type' => 'details',
105 '#title' => t('Clear cache'),
109 $form['clear_cache']['clear'] = [
111 '#value' => t('Clear all caches'),
112 '#submit' => ['::submitCacheClear'],
116 '#type' => 'details',
117 '#title' => t('Caching'),
119 '#description' => $this->t('Note: Drupal provides an internal page cache module that is recommended for small to medium-sized websites.'),
121 // Identical options to the ones for block caching.
122 // @see \Drupal\Core\Block\BlockBase::buildConfigurationForm()
123 $period = [0, 60, 180, 300, 600, 900, 1800, 2700, 3600, 10800, 21600, 32400, 43200, 86400];
124 $period = array_map([$this->dateFormatter, 'formatInterval'], array_combine($period, $period));
125 $period[0] = '<' . t('no caching') . '>';
126 $form['caching']['page_cache_maximum_age'] = [
128 '#title' => t('Page cache maximum age'),
129 '#default_value' => $config->get('cache.page.max_age'),
130 '#options' => $period,
131 '#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.'),
134 $directory = 'public://';
135 $is_writable = is_dir($directory) && is_writable($directory);
136 $disabled = !$is_writable;
137 $disabled_message = '';
139 $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')]);
142 $form['bandwidth_optimization'] = [
143 '#type' => 'details',
144 '#title' => t('Bandwidth optimization'),
146 '#description' => t('External resources can be optimized automatically, which can reduce both the size and number of requests made to your website.') . $disabled_message,
149 $form['bandwidth_optimization']['preprocess_css'] = [
150 '#type' => 'checkbox',
151 '#title' => t('Aggregate CSS files'),
152 '#default_value' => $config->get('css.preprocess'),
153 '#disabled' => $disabled,
155 $form['bandwidth_optimization']['preprocess_js'] = [
156 '#type' => 'checkbox',
157 '#title' => t('Aggregate JavaScript files'),
158 '#default_value' => $config->get('js.preprocess'),
159 '#disabled' => $disabled,
162 return parent::buildForm($form, $form_state);
168 public function submitForm(array &$form, FormStateInterface $form_state) {
169 $this->cssCollectionOptimizer->deleteAll();
170 $this->jsCollectionOptimizer->deleteAll();
171 // This form allows page compression settings to be changed, which can
172 // invalidate cached pages in the render cache, so it needs to be cleared on
174 $this->renderCache->deleteAll();
176 $this->config('system.performance')
177 ->set('cache.page.max_age', $form_state->getValue('page_cache_maximum_age'))
178 ->set('css.preprocess', $form_state->getValue('preprocess_css'))
179 ->set('js.preprocess', $form_state->getValue('preprocess_js'))
182 parent::submitForm($form, $form_state);
188 public function submitCacheClear(array &$form, FormStateInterface $form_state) {
189 drupal_flush_all_caches();
190 drupal_set_message(t('Caches cleared.'));