3 namespace Drupal\advagg_bundler\Form;
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;
13 * Configure advagg_bundler settings for this site.
15 class SettingsForm extends ConfigFormBase {
18 * The CSS asset collection optimizer service.
20 * @var \Drupal\Core\Asset\AssetCollectionOptimizerInterface
22 protected $cssCollectionOptimizer;
25 * The JavaScript asset collection optimizer service.
27 * @var \Drupal\Core\Asset\AssetCollectionOptimizerInterface
29 protected $jsCollectionOptimizer;
32 * Constructs a SettingsForm object.
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.
41 public function __construct(ConfigFactoryInterface $config_factory, AssetCollectionOptimizerInterface $css_collection_optimizer, AssetCollectionOptimizerInterface $js_collection_optimizer) {
42 parent::__construct($config_factory);
44 $this->cssCollectionOptimizer = $css_collection_optimizer;
45 $this->jsCollectionOptimizer = $js_collection_optimizer;
51 public static function create(ContainerInterface $container) {
53 $container->get('config.factory'),
54 $container->get('asset.css.collection_optimizer'),
55 $container->get('asset.js.collection_optimizer')
62 public function getFormId() {
63 return 'advagg_bundler_settings';
69 protected function getEditableConfigNames() {
70 return ['advagg_bundler.settings'];
76 public function buildForm(array $form, FormStateInterface $form_state) {
77 $config = $this->config('advagg_bundler.settings');
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.'),
105 '#type' => 'fieldset',
106 '#title' => $this->t('CSS Bundling options.'),
108 $form['css']['max_css'] = [
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'),
116 '#edit-active' => ['checked' => FALSE],
120 $form['css']['css_logic'] = [
122 '#title' => $this->t('Grouping logic'),
123 '#default_value' => $config->get('css_logic'),
125 0 => $this->t('File count'),
126 1 => $this->t('File size'),
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.'),
131 '#edit-active' => ['checked' => FALSE],
137 '#type' => 'fieldset',
138 '#title' => $this->t('JavaScript Bundling options.'),
140 $form['js']['max_js'] = [
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'),
148 '#edit-active' => ['checked' => FALSE],
152 $form['js']['js_logic'] = [
154 '#title' => $this->t('Grouping logic'),
155 '#default_value' => $config->get('js_logic'),
157 0 => $this->t('File count'),
158 1 => $this->t('File size'),
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.'),
163 '#edit-active' => ['checked' => FALSE],
168 return parent::buildForm($form, $form_state);
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'))
182 parent::submitForm($form, $form_state);
184 // Clear relevant caches.
185 $this->cssCollectionOptimizer->deleteAll();
186 $this->jsCollectionOptimizer->deleteAll();
187 Cache::invalidateTags([