Interim commit.
[yaffs-website] / web / modules / contrib / advagg / advagg_bundler / src / Form / SettingsForm.php
1 <?php
2
3 namespace Drupal\advagg_bundler\Form;
4
5 use Drupal\Core\Asset\AssetCollectionOptimizerInterface;
6 use Drupal\Core\Cache\Cache;
7 use Drupal\Core\Config\ConfigFactoryInterface;
8 use Drupal\Core\Form\ConfigFormBase;
9 use Drupal\Core\Form\FormStateInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Configure advagg_bundler settings for this site.
14  */
15 class SettingsForm extends ConfigFormBase {
16
17   /**
18    * The CSS asset collection optimizer service.
19    *
20    * @var \Drupal\Core\Asset\AssetCollectionOptimizerInterface
21    */
22   protected $cssCollectionOptimizer;
23
24   /**
25    * The JavaScript asset collection optimizer service.
26    *
27    * @var \Drupal\Core\Asset\AssetCollectionOptimizerInterface
28    */
29   protected $jsCollectionOptimizer;
30
31   /**
32    * Constructs a SettingsForm object.
33    *
34    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
35    *   The factory for configuration objects.
36    * @param \Drupal\Core\Asset\AssetCollectionOptimizerInterface $css_collection_optimizer
37    *   The CSS asset collection optimizer service.
38    * @param \Drupal\Core\Asset\AssetCollectionOptimizerInterface $js_collection_optimizer
39    *   The JavaScript asset collection optimizer service.
40    */
41   public function __construct(ConfigFactoryInterface $config_factory, AssetCollectionOptimizerInterface $css_collection_optimizer, AssetCollectionOptimizerInterface $js_collection_optimizer) {
42     parent::__construct($config_factory);
43
44     $this->cssCollectionOptimizer = $css_collection_optimizer;
45     $this->jsCollectionOptimizer = $js_collection_optimizer;
46   }
47
48   /**
49    * {@inheritdoc}
50    */
51   public static function create(ContainerInterface $container) {
52     return new static(
53       $container->get('config.factory'),
54       $container->get('asset.css.collection_optimizer'),
55       $container->get('asset.js.collection_optimizer')
56     );
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public function getFormId() {
63     return 'advagg_bundler_settings';
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   protected function getEditableConfigNames() {
70     return ['advagg_bundler.settings'];
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function buildForm(array $form, FormStateInterface $form_state) {
77     $config = $this->config('advagg_bundler.settings');
78     $form = [];
79     $form['active'] = [
80       '#type' => 'checkbox',
81       '#title' => $this->t('Bundler is Active'),
82       '#default_value' => $config->get('active'),
83       '#description' => $this->t('If not checked, the bundler will not split up aggregates.'),
84     ];
85
86     $options = [
87       0 => 0,
88       1 => 1,
89       2 => 2,
90       3 => 3,
91       4 => 4,
92       5 => 5,
93       6 => 6,
94       7 => 7,
95       8 => 8,
96       9 => 9,
97       10 => 10,
98       11 => 11,
99       12 => 12,
100       13 => 13,
101       14 => 14,
102       15 => 15,
103     ];
104     $form['css'] = [
105       '#type' => 'fieldset',
106       '#title' => $this->t('CSS Bundling options.'),
107     ];
108     $form['css']['max_css'] = [
109       '#type' => 'select',
110       '#title' => $this->t('Target Number Of CSS Bundles Per Page'),
111       '#default_value' => $config->get('max_css'),
112       '#options' => $options,
113       '#description' => $this->t('If 0 is selected then the bundler is disabled'),
114       '#states' => [
115         'disabled' => [
116           '#edit-active' => ['checked' => FALSE],
117         ],
118       ],
119     ];
120     $form['css']['css_logic'] = [
121       '#type' => 'radios',
122       '#title' => $this->t('Grouping logic'),
123       '#default_value' => $config->get('css_logic'),
124       '#options' => [
125         0 => $this->t('File count'),
126         1 => $this->t('File size'),
127       ],
128       '#description' => $this->t('If file count is selected then each bundle will try to have a similar number of original files aggregated inside of it. If file size is selected then each bundle will try to have a similar file size.'),
129       '#states' => [
130         'disabled' => [
131           '#edit-active' => ['checked' => FALSE],
132         ],
133       ],
134     ];
135
136     $form['js'] = [
137       '#type' => 'fieldset',
138       '#title' => $this->t('JavaScript Bundling options.'),
139     ];
140     $form['js']['max_js'] = [
141       '#type' => 'select',
142       '#title' => $this->t('Target Number Of JS Bundles Per Page'),
143       '#default_value' => $config->get('max_js'),
144       '#options' => $options,
145       '#description' => $this->t('If 0 is selected then the bundler is disabled'),
146       '#states' => [
147         'disabled' => [
148           '#edit-active' => ['checked' => FALSE],
149         ],
150       ],
151     ];
152     $form['js']['js_logic'] = [
153       '#type' => 'radios',
154       '#title' => $this->t('Grouping logic'),
155       '#default_value' => $config->get('js_logic'),
156       '#options' => [
157         0 => $this->t('File count'),
158         1 => $this->t('File size'),
159       ],
160       '#description' => $this->t('If file count is selected then each bundle will try to have a similar number of original files aggregated inside of it. If file size is selected then each bundle will try to have a similar file size.'),
161       '#states' => [
162         'disabled' => [
163           '#edit-active' => ['checked' => FALSE],
164         ],
165       ],
166     ];
167
168     return parent::buildForm($form, $form_state);
169   }
170
171   /**
172    * {@inheritdoc}
173    */
174   public function submitForm(array &$form, FormStateInterface $form_state) {
175     $this->config('advagg_bundler.settings')
176       ->set('active', $form_state->getValue('active'))
177       ->set('max_css', $form_state->getValue('max_css'))
178       ->set('css_logic', $form_state->getValue('css_logic'))
179       ->set('max_js', $form_state->getValue('max_js'))
180       ->set('js_logic', $form_state->getValue('js_logic'))
181       ->save();
182     parent::submitForm($form, $form_state);
183
184     // Clear relevant caches.
185     $this->cssCollectionOptimizer->deleteAll();
186     $this->jsCollectionOptimizer->deleteAll();
187     Cache::invalidateTags([
188       'library_info',
189       'advagg_css',
190       'advagg_js',
191     ]);
192   }
193
194 }