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.
17 class PerformanceForm extends ConfigFormBase {
20 * The date formatter service.
22 * @var \Drupal\Core\Datetime\DateFormatterInterface
24 protected $dateFormatter;
27 * The CSS asset collection optimizer service.
29 * @var \Drupal\Core\Asset\AssetCollectionOptimizerInterface
31 protected $cssCollectionOptimizer;
34 * The JavaScript asset collection optimizer service.
36 * @var \Drupal\Core\Asset\AssetCollectionOptimizerInterface
38 protected $jsCollectionOptimizer;
41 * Constructs a PerformanceForm object.
43 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
44 * The factory for configuration objects.
45 * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
46 * The date formatter service.
47 * @param \Drupal\Core\Asset\AssetCollectionOptimizerInterface $css_collection_optimizer
48 * The CSS asset collection optimizer service.
49 * @param \Drupal\Core\Asset\AssetCollectionOptimizerInterface $js_collection_optimizer
50 * The JavaScript asset collection optimizer service.
52 public function __construct(ConfigFactoryInterface $config_factory, DateFormatterInterface $date_formatter, AssetCollectionOptimizerInterface $css_collection_optimizer, AssetCollectionOptimizerInterface $js_collection_optimizer) {
53 parent::__construct($config_factory);
55 $this->dateFormatter = $date_formatter;
56 $this->cssCollectionOptimizer = $css_collection_optimizer;
57 $this->jsCollectionOptimizer = $js_collection_optimizer;
63 public static function create(ContainerInterface $container) {
65 $container->get('config.factory'),
66 $container->get('date.formatter'),
67 $container->get('asset.css.collection_optimizer'),
68 $container->get('asset.js.collection_optimizer')
75 public function getFormId() {
76 return 'system_performance_settings';
82 protected function getEditableConfigNames() {
83 return ['system.performance'];
89 public function buildForm(array $form, FormStateInterface $form_state) {
90 $form['#attached']['library'][] = 'system/drupal.system';
92 $config = $this->config('system.performance');
94 $form['clear_cache'] = [
96 '#title' => t('Clear cache'),
100 $form['clear_cache']['clear'] = [
102 '#value' => t('Clear all caches'),
103 '#submit' => ['::submitCacheClear'],
107 '#type' => 'details',
108 '#title' => t('Caching'),
110 '#description' => $this->t('Note: Drupal provides an internal page cache module that is recommended for small to medium-sized websites.'),
112 // Identical options to the ones for block caching.
113 // @see \Drupal\Core\Block\BlockBase::buildConfigurationForm()
114 $period = [0, 60, 180, 300, 600, 900, 1800, 2700, 3600, 10800, 21600, 32400, 43200, 86400];
115 $period = array_map([$this->dateFormatter, 'formatInterval'], array_combine($period, $period));
116 $period[0] = '<' . t('no caching') . '>';
117 $form['caching']['page_cache_maximum_age'] = [
119 '#title' => t('Page cache maximum age'),
120 '#default_value' => $config->get('cache.page.max_age'),
121 '#options' => $period,
122 '#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.'),
125 $directory = 'public://';
126 $is_writable = is_dir($directory) && is_writable($directory);
127 $disabled = !$is_writable;
128 $disabled_message = '';
130 $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')]);
133 $form['bandwidth_optimization'] = [
134 '#type' => 'details',
135 '#title' => t('Bandwidth optimization'),
137 '#description' => t('External resources can be optimized automatically, which can reduce both the size and number of requests made to your website.') . $disabled_message,
140 $form['bandwidth_optimization']['preprocess_css'] = [
141 '#type' => 'checkbox',
142 '#title' => t('Aggregate CSS files'),
143 '#default_value' => $config->get('css.preprocess'),
144 '#disabled' => $disabled,
146 $form['bandwidth_optimization']['preprocess_js'] = [
147 '#type' => 'checkbox',
148 '#title' => t('Aggregate JavaScript files'),
149 '#default_value' => $config->get('js.preprocess'),
150 '#disabled' => $disabled,
153 return parent::buildForm($form, $form_state);
159 public function submitForm(array &$form, FormStateInterface $form_state) {
160 $this->cssCollectionOptimizer->deleteAll();
161 $this->jsCollectionOptimizer->deleteAll();
163 $this->config('system.performance')
164 ->set('cache.page.max_age', $form_state->getValue('page_cache_maximum_age'))
165 ->set('css.preprocess', $form_state->getValue('preprocess_css'))
166 ->set('js.preprocess', $form_state->getValue('preprocess_js'))
169 parent::submitForm($form, $form_state);
175 public function submitCacheClear(array &$form, FormStateInterface $form_state) {
176 drupal_flush_all_caches();
177 drupal_set_message(t('Caches cleared.'));