3 namespace Drupal\advagg_js_minify\Form;
5 use Drupal\Component\Utility\Xss;
6 use Drupal\Core\Asset\AssetCollectionOptimizerInterface;
7 use Drupal\Core\Asset\AssetOptimizerInterface;
8 use Drupal\Core\Cache\Cache;
9 use Drupal\Core\Config\ConfigFactoryInterface;
10 use Drupal\Core\Form\ConfigFormBase;
11 use Drupal\Core\Form\FormStateInterface;
12 use Drupal\Core\State\StateInterface;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
17 * Configure advagg_js_minify settings for this site.
19 class SettingsForm extends ConfigFormBase {
22 * The JavaScript asset collection optimizer service.
24 * @var \Drupal\Core\Asset\AssetCollectionOptimizerInterface
26 protected $jsCollectionOptimizer;
29 * The AdvAgg file status state information storage service.
31 * @var \Drupal\Core\State\StateInterface
33 protected $advaggFiles;
36 * A JS asset optimizer.
38 * @var \Drupal\advagg_js_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 $js_collection_optimizer
48 * The JavaScript 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 JS asset.
54 public function __construct(ConfigFactoryInterface $config_factory, AssetCollectionOptimizerInterface $js_collection_optimizer, StateInterface $advagg_files, AssetOptimizerInterface $optimizer) {
55 parent::__construct($config_factory);
56 $this->jsCollectionOptimizer = $js_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.js.collection_optimizer'),
68 $container->get('state.advagg.files'),
69 $container->get('asset.js.optimizer')
76 public function getFormId() {
77 return 'advagg_js_minify_settings';
83 protected function getEditableConfigNames() {
84 return ['advagg_js_minify.settings'];
90 public function buildForm(array $form, FormStateInterface $form_state) {
92 $config = $this->config('advagg_js_minify.settings');
94 if ($this->config('advagg.settings')->get('cache_level') < 0) {
95 $form['advagg_devel_msg'] = [
96 '#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 aggressive, JS minification will take place.', [
97 '@devel' => Url::fromRoute('advagg.settings', [], [
98 'fragment' => 'edit-advagg-cache-level',
104 list($options, $description) = $this->optimizer->getConfiguration();
106 $form['minifier'] = [
108 '#title' => $this->t('Minification: Select a minifier'),
109 '#default_value' => $config->get('minifier'),
110 '#options' => $options,
111 '#description' => Xss::filter($description),
113 $form['add_license'] = [
114 '#type' => 'checkbox',
115 '#title' => $this->t('Add licensing comments'),
116 '#default_value' => $config->get('add_license'),
117 '#description' => $this->t("If unchecked, the Advanced Aggregation module's licensing comments
118 will be omitted from the aggregated files. Omitting the comments will produce somewhat better scores in
119 some automated security scans but otherwise should not affect your site. These are included by default in order to better follow the spirit of the GPL by providing the source for javascript files."),
122 $options[-1] = $this->t('Default');
125 $form['per_file_settings'] = [
126 '#type' => 'details',
127 '#title' => $this->t('Per File Settings'),
129 $files = $this->advaggFiles->getAll();
130 $file_settings = $config->get('file_settings');
131 if ($file_settings) {
132 $file_settings = array_column($file_settings, 'minifier', 'path');
134 foreach ($files as $name => $info) {
135 if ($info['fileext'] !== 'js') {
138 $dir = dirname($name);
139 if (!isset($form['per_file_settings'][$dir])) {
140 $form['per_file_settings'][$dir] = [
141 '#type' => 'details',
145 $form_api_filename = str_replace(['/', '.'], ['__', '--'], $name);
146 $form['per_file_settings'][$dir]['advagg_js_minify_file_settings_' . $form_api_filename] = [
148 '#title' => $this->t('%filename: Select a minifier', ['%filename' => $name]),
149 '#default_value' => isset($file_settings[$name]) ? $file_settings[$name] : -1,
150 '#options' => $options,
152 if ($form['per_file_settings'][$dir]['advagg_js_minify_file_settings_' . $form_api_filename]['#default_value'] != -1) {
153 $form['per_file_settings'][$dir]['#open'] = TRUE;
154 $form['per_file_settings']['#open'] = TRUE;
158 // No js files are found.
160 $form['per_file_settings']['#description'] = $this->t('No JS files have been aggregated. You need to enable aggregation. No js files where found in the advagg_files table.');
163 return parent::buildForm($form, $form_state);
169 public function submitForm(array &$form, FormStateInterface $form_state) {
170 $config = $this->config('advagg_js_minify.settings');
172 // Extract/combine per file settings.
173 if ($file_settings = $config->get('file_settings')) {
174 $file_settings = array_column($file_settings, NULL, 'path');
179 foreach ($form_state->getValues() as $key => $value) {
180 // Skip if not a advagg_js_minify_file_settings form item.
181 if (strpos($key, 'advagg_js_minify_file_settings_') === FALSE) {
184 $path = str_replace(['__', '--'], ['/', '.'], substr($key, 31));
186 unset($file_settings[$path]);
190 $file_settings[$path] = [
192 'minifier' => $value,
198 $this->jsCollectionOptimizer->deleteAll();
199 Cache::invalidateTags(['library_info', 'advagg_js']);
202 $config->set('add_license', $form_state->getValue('add_license'))
203 ->set('minifier', $form_state->getValue('minifier'))
204 ->set('file_settings', array_values($file_settings))
206 parent::submitForm($form, $form_state);