Yaffs site version 1.1
[yaffs-website] / web / modules / contrib / advagg / advagg_css_minify / src / Form / SettingsForm.php
1 <?php
2
3 namespace Drupal\advagg_css_minify\Form;
4
5 use Drupal\Core\Asset\AssetCollectionOptimizerInterface;
6 use Drupal\Core\Asset\AssetOptimizerInterface;
7 use Drupal\Core\Cache\Cache;
8 use Drupal\Core\Config\ConfigFactoryInterface;
9 use Drupal\Core\Form\ConfigFormBase;
10 use Drupal\Core\Form\FormStateInterface;
11 use Drupal\Core\State\StateInterface;
12 use Drupal\Core\Url;
13 use Drupal\Component\Utility\Xss;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
15
16 /**
17  * Configure advagg_css_minify settings for this site.
18  */
19 class SettingsForm extends ConfigFormBase {
20
21   /**
22    * The CSS asset collection optimizer service.
23    *
24    * @var \Drupal\Core\Asset\AssetCollectionOptimizerInterface
25    */
26   protected $cssCollectionOptimizer;
27
28   /**
29    * The AdvAgg file status state information storage service.
30    *
31    * @var \Drupal\Core\State\StateInterface
32    */
33   protected $advaggFiles;
34
35   /**
36    * A CSS asset optimizer.
37    *
38    * @var \Drupal\advagg_css_minify\Asset\CssOptimizer
39    */
40   protected $optimizer;
41
42   /**
43    * Constructs a SettingsForm object.
44    *
45    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
46    *   The factory for configuration objects.
47    * @param \Drupal\Core\Asset\AssetCollectionOptimizerInterface $css_collection_optimizer
48    *   The CSS asset collection optimizer service.
49    * @param \Drupal\Core\State\StateInterface $advagg_files
50    *   The AdvAgg file status state information storage service.
51    * @param \Drupal\Core\Asset\AssetOptimizerInterface $optimizer
52    *   The optimizer for a single CSS asset.
53    */
54   public function __construct(ConfigFactoryInterface $config_factory, AssetCollectionOptimizerInterface $css_collection_optimizer, StateInterface $advagg_files, AssetOptimizerInterface $optimizer) {
55     parent::__construct($config_factory);
56     $this->cssCollectionOptimizer = $css_collection_optimizer;
57     $this->advaggFiles = $advagg_files;
58     $this->optimizer = $optimizer;
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public static function create(ContainerInterface $container) {
65     return new static(
66       $container->get('config.factory'),
67       $container->get('asset.css.collection_optimizer'),
68       $container->get('state.advagg.files'),
69       $container->get('asset.css.optimizer')
70     );
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function getFormId() {
77     return 'advagg_css_minify_settings';
78   }
79
80   /**
81    * {@inheritdoc}
82    */
83   protected function getEditableConfigNames() {
84     return ['advagg_css_minify.settings'];
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   public function buildForm(array $form, FormStateInterface $form_state) {
91     $config = $this->config('advagg_css_minify.settings');
92     $form = [];
93     if ($this->config('advagg.settings')->get('cache_level') < 0) {
94       $form['advagg_devel_msg'] = [
95         '#markup' => '<p>' . $this->t('The settings below will not have any effect because AdvAgg is currently in <a href="@devel">development mode</a>. Once the cache settings have been set to normal or higher, CSS minification will take place.', [
96           '@devel' => Url::fromRoute('advagg.settings', [], [
97             'fragment' => 'edit-advagg-cache-level',
98           ])->toString(),
99         ]) . '</p>',
100       ];
101     }
102     if (method_exists($this->optimizer, 'getConfiguration')) {
103       list($options, $description) = $this->optimizer->getConfiguration();
104     }
105     else {
106       $description = '';
107       $options = [
108         0 => $this->t('Disabled'),
109         1 => $this->t('Core'),
110         2 => $this->t('YUI'),
111       ];
112
113       // Allow for other modules to alter this list.
114       $options_desc = [$options, $description];
115       list($options, $description) = $options_desc;
116     }
117
118     $form['minifier'] = [
119       '#type' => 'radios',
120       '#title' => $this->t('Minification: Select a default minifier'),
121       '#default_value' => $config->get('minifier'),
122       '#options' => $options,
123       '#description' => Xss::filter($description),
124     ];
125
126     $options[-1] = $this->t('Default');
127     ksort($options);
128
129     $form['per_file_settings'] = [
130       '#type' => 'details',
131       '#title' => $this->t('Per File Settings'),
132     ];
133     $files = $this->advaggFiles->getAll();
134     $file_settings = $config->get('file_settings');
135     if ($file_settings) {
136       $file_settings = array_column($file_settings, 'minifier', 'path');
137     }
138     foreach ($files as $name => $info) {
139       if ($info['fileext'] !== 'css') {
140         continue;
141       }
142       $dir = dirname($name);
143       if (!isset($form['per_file_settings'][$dir])) {
144         $form['per_file_settings'][$dir] = [
145           '#type' => 'details',
146           '#title' => $dir,
147         ];
148       }
149       $form_api_filename = str_replace(['/', '.'], ['__', '--'], $name);
150       $form['per_file_settings'][$dir]['advagg_css_minify_file_settings_' . $form_api_filename] = [
151         '#type' => 'radios',
152         '#title' => $this->t('%filename: Select a minifier', ['%filename' => $name]),
153         '#default_value' => isset($file_settings[$name]) ? $file_settings[$name] : -1,
154         '#options' => $options,
155       ];
156       if ($form['per_file_settings'][$dir]['advagg_css_minify_file_settings_' . $form_api_filename]['#default_value'] != -1) {
157         $form['per_file_settings'][$dir]['#open'] = TRUE;
158         $form['per_file_settings']['#open'] = TRUE;
159       }
160     }
161
162     // No css files are found.
163     if (empty($files)) {
164       $form['per_file_settings']['#description'] = $this->t('No CSS files have been aggregated. You need to enable aggregation. No css files where found in the advagg_files table.');
165     }
166     return parent::buildForm($form, $form_state);
167   }
168
169   /**
170    * {@inheritdoc}
171    */
172   public function submitForm(array &$form, FormStateInterface $form_state) {
173     $config = $this->config('advagg_css_minify.settings');
174
175     // Extract/combine per file settings.
176     if ($file_settings = $config->get('file_settings')) {
177       $file_settings = array_column($file_settings, NULL, 'path');
178     }
179     else {
180       $file_settings = [];
181     }
182     foreach ($form_state->getValues() as $key => $value) {
183       // Skip if not advagg_css_minify_file_settings.
184       if (strpos($key, 'advagg_css_minify_file_settings_') === FALSE) {
185         continue;
186       }
187       $path = str_replace(['__', '--'], ['/', '.'], substr($key, 32));
188       if ($value == -1) {
189         unset($file_settings[$path]);
190         continue;
191       }
192       else {
193         $file_settings[$path] = [
194           'path' => $path,
195           'minifier' => $value,
196         ];
197       }
198     }
199
200     // Clear relevant caches.
201     $this->cssCollectionOptimizer->deleteAll();
202     Cache::invalidateTags(['library_info', 'advagg_css']);
203
204     $config = $this->config('advagg_css_minify.settings')
205       ->set('minifier', $form_state->getValue('minifier'))
206       ->set('file_settings', array_values($file_settings))
207       ->save();
208     parent::submitForm($form, $form_state);
209   }
210
211 }