3 namespace Drupal\system\Form;
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;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
15 * Configure performance settings for this site.
19 class PerformanceForm extends ConfigFormBase {
22 * The date formatter service.
24 * @var \Drupal\Core\Datetime\DateFormatterInterface
26 protected $dateFormatter;
29 * The CSS asset collection optimizer service.
31 * @var \Drupal\Core\Asset\AssetCollectionOptimizerInterface
33 protected $cssCollectionOptimizer;
36 * The JavaScript asset collection optimizer service.
38 * @var \Drupal\Core\Asset\AssetCollectionOptimizerInterface
40 protected $jsCollectionOptimizer;
45 * @var \Drupal\Core\Extension\ModuleHandlerInterface
47 protected $moduleHandler;
50 * Constructs a PerformanceForm object.
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
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);
66 $this->dateFormatter = $date_formatter;
67 $this->cssCollectionOptimizer = $css_collection_optimizer;
68 $this->jsCollectionOptimizer = $js_collection_optimizer;
69 $this->moduleHandler = $module_handler;
75 public static function create(ContainerInterface $container) {
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')
88 public function getFormId() {
89 return 'system_performance_settings';
95 protected function getEditableConfigNames() {
96 return ['system.performance'];
102 public function buildForm(array $form, FormStateInterface $form_state) {
103 $form['#attached']['library'][] = 'system/drupal.system';
105 $config = $this->config('system.performance');
107 $form['clear_cache'] = [
108 '#type' => 'details',
109 '#title' => t('Clear cache'),
113 $form['clear_cache']['clear'] = [
115 '#value' => t('Clear all caches'),
116 '#submit' => ['::submitCacheClear'],
120 '#type' => 'details',
121 '#title' => t('Caching'),
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'] = [
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.'),
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'),
141 $directory = 'public://';
142 $is_writable = is_dir($directory) && is_writable($directory);
143 $disabled = !$is_writable;
144 $disabled_message = '';
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')]);
149 $form['bandwidth_optimization'] = [
150 '#type' => 'details',
151 '#title' => t('Bandwidth optimization'),
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,
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,
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,
169 return parent::buildForm($form, $form_state);
175 public function submitForm(array &$form, FormStateInterface $form_state) {
176 $this->cssCollectionOptimizer->deleteAll();
177 $this->jsCollectionOptimizer->deleteAll();
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'))
185 parent::submitForm($form, $form_state);
191 public function submitCacheClear(array &$form, FormStateInterface $form_state) {
192 drupal_flush_all_caches();
193 $this->messenger()->addStatus($this->t('Caches cleared.'));