3 namespace Drupal\advagg_css_minify\Form;
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;
13 use Drupal\Component\Utility\Xss;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
17 * Configure advagg_css_minify settings for this site.
19 class SettingsForm extends ConfigFormBase {
22 * The CSS asset collection optimizer service.
24 * @var \Drupal\Core\Asset\AssetCollectionOptimizerInterface
26 protected $cssCollectionOptimizer;
29 * The AdvAgg file status state information storage service.
31 * @var \Drupal\Core\State\StateInterface
33 protected $advaggFiles;
36 * A CSS asset optimizer.
38 * @var \Drupal\advagg_css_minify\Asset\CssOptimizer
43 * Constructs a SettingsForm object.
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.
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;
64 public static function create(ContainerInterface $container) {
66 $container->get('config.factory'),
67 $container->get('asset.css.collection_optimizer'),
68 $container->get('state.advagg.files'),
69 $container->get('asset.css.optimizer')
76 public function getFormId() {
77 return 'advagg_css_minify_settings';
83 protected function getEditableConfigNames() {
84 return ['advagg_css_minify.settings'];
90 public function buildForm(array $form, FormStateInterface $form_state) {
91 $config = $this->config('advagg_css_minify.settings');
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',
102 if (method_exists($this->optimizer, 'getConfiguration')) {
103 list($options, $description) = $this->optimizer->getConfiguration();
108 0 => $this->t('Disabled'),
109 1 => $this->t('Core'),
110 2 => $this->t('YUI'),
113 // Allow for other modules to alter this list.
114 $options_desc = [$options, $description];
115 list($options, $description) = $options_desc;
118 $form['minifier'] = [
120 '#title' => $this->t('Minification: Select a default minifier'),
121 '#default_value' => $config->get('minifier'),
122 '#options' => $options,
123 '#description' => Xss::filter($description),
126 $options[-1] = $this->t('Default');
129 $form['per_file_settings'] = [
130 '#type' => 'details',
131 '#title' => $this->t('Per File Settings'),
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');
138 foreach ($files as $name => $info) {
139 if ($info['fileext'] !== 'css') {
142 $dir = dirname($name);
143 if (!isset($form['per_file_settings'][$dir])) {
144 $form['per_file_settings'][$dir] = [
145 '#type' => 'details',
149 $form_api_filename = str_replace(['/', '.'], ['__', '--'], $name);
150 $form['per_file_settings'][$dir]['advagg_css_minify_file_settings_' . $form_api_filename] = [
152 '#title' => $this->t('%filename: Select a minifier', ['%filename' => $name]),
153 '#default_value' => isset($file_settings[$name]) ? $file_settings[$name] : -1,
154 '#options' => $options,
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;
162 // No css files are found.
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.');
166 return parent::buildForm($form, $form_state);
172 public function submitForm(array &$form, FormStateInterface $form_state) {
173 $config = $this->config('advagg_css_minify.settings');
175 // Extract/combine per file settings.
176 if ($file_settings = $config->get('file_settings')) {
177 $file_settings = array_column($file_settings, NULL, 'path');
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) {
187 $path = str_replace(['__', '--'], ['/', '.'], substr($key, 32));
189 unset($file_settings[$path]);
193 $file_settings[$path] = [
195 'minifier' => $value,
200 // Clear relevant caches.
201 $this->cssCollectionOptimizer->deleteAll();
202 Cache::invalidateTags(['library_info', 'advagg_css']);
204 $config = $this->config('advagg_css_minify.settings')
205 ->set('minifier', $form_state->getValue('minifier'))
206 ->set('file_settings', array_values($file_settings))
208 parent::submitForm($form, $form_state);