Version 1
[yaffs-website] / web / modules / contrib / advagg / advagg_cdn / src / Form / SettingsForm.php
1 <?php
2
3 namespace Drupal\advagg_cdn\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_js_minify 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    * {@inheritdoc}
33    *
34    * @param \Drupal\Core\Asset\AssetCollectionOptimizerInterface $css_collection_optimizer
35    *   The CSS asset collection optimizer service.
36    * @param \Drupal\Core\Asset\AssetCollectionOptimizerInterface $js_collection_optimizer
37    *   The JavaScript asset collection optimizer service.
38    */
39   public function __construct(ConfigFactoryInterface $config_factory, AssetCollectionOptimizerInterface $css_collection_optimizer, AssetCollectionOptimizerInterface $js_collection_optimizer) {
40     parent::__construct($config_factory);
41
42     $this->cssCollectionOptimizer = $css_collection_optimizer;
43     $this->jsCollectionOptimizer = $js_collection_optimizer;
44   }
45
46   /**
47    * {@inheritdoc}
48    */
49   public static function create(ContainerInterface $container) {
50     return new static(
51       $container->get('config.factory'),
52       $container->get('asset.css.collection_optimizer'),
53       $container->get('asset.js.collection_optimizer')
54     );
55   }
56
57   /**
58    * {@inheritdoc}
59    */
60   public function getFormId() {
61     return 'advagg_cdn_settings';
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   protected function getEditableConfigNames() {
68     return ['advagg_cdn.settings'];
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function buildForm(array $form, FormStateInterface $form_state) {
75     $config = $this->config('advagg_cdn.settings');
76     $form = [];
77
78     $form['cdn'] = [
79       '#type' => 'radios',
80       '#title' => $this->t('CDN to use'),
81       '#default_value' => $config->get('cdn'),
82       '#options' => ['google' => 'Google', 'microsoft' => 'Microsoft'],
83     ];
84     $form['minified'] = [
85       '#type' => 'checkbox',
86       '#title' => $this->t('Use Minified Resources'),
87       '#default_value' => $config->get('minified'),
88       '#description' => $this->t('When available use minified versions of any resources being served by the CDN.'),
89     ];
90     if ($this->config('advagg.settings')->get('cache_level') < 0) {
91       $form['minified']['#description'] .= $this->t('This setting 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.', ['@devel' => Url::fromRoute('advagg.settings', ['fragment' => 'edit-advagg-cache-level'])->toString()]);
92     }
93
94     $form['jquery'] = [
95       '#type' => 'details',
96       '#title' => 'jQuery',
97     ];
98     $form['jquery']['jquery_active'] = [
99       '#type' => 'checkbox',
100       '#title' => $this->t('Serve jQuery by CDN'),
101       '#default_value' => $config->get('jquery'),
102     ];
103     $form['jquery']['jquery_version'] = [
104       '#type' => 'textfield',
105       '#title' => $this->t('jQuery version'),
106       '#default_value' => $config->get('jquery_version'),
107       '#description' => $this->t('Version of jQuery to load.'),
108       '#states' => [
109         'disabled' => [
110           ':input[name="jquery_active"]' => ['value' => "0"],
111         ],
112       ],
113     ];
114     $form['jquery_ui'] = [
115       '#type' => 'details',
116       '#title' => 'jQuery UI',
117     ];
118     $form['jquery_ui']['jquery_ui_css'] = [
119       '#type' => 'checkbox',
120       '#title' => $this->t('Serve jQuery UI CSS by CDN'),
121       '#default_value' => $config->get('jquery_ui_css'),
122       '#description' => $this->t('Warning: this may change your site appearance as Drupal 8 by default uses the base jQuery theme and the base theme is not available by CDN.'),
123     ];
124     $jquery_themes = [
125       'black-tie',
126       'blitzer',
127       'cupertino',
128       'dark-hive',
129       'dot-luv',
130       'eggplant',
131       'excite-bike',
132       'flick',
133       'hot-sneaks',
134       'humanity',
135       'le-frog',
136       'mint-choc',
137       'overcast',
138       'pepper-grinder',
139       'redmond',
140       'smoothness',
141       'south-street',
142       'start',
143       'sunny',
144       'swanky-purse',
145       'trontastic',
146       'ui-darkness',
147       'ui-lightness',
148       'vader',
149     ];
150     $form['jquery_ui']['jquery_ui_theme'] = [
151       '#type' => 'select',
152       '#title' => $this->t('jQuery UI theme'),
153       '#default_value' => $config->get('jquery_ui_theme'),
154       '#description' => $this->t('Choose which jQuery theme to use. Smoothness is the most basic and similar to Drupal standard version.'),
155       '#options' => array_combine($jquery_themes, $jquery_themes),
156     ];
157     $form['jquery_ui']['jquery_ui_js'] = [
158       '#type' => 'checkbox',
159       '#title' => $this->t('Serve jQuery UI JavaScript by CDN'),
160       '#default_value' => $config->get('jquery_ui_js'),
161     ];
162     $form['jquery_ui']['jquery_ui_version'] = [
163       '#type' => 'textfield',
164       '#title' => $this->t('jQuery UI version'),
165       '#default_value' => $config->get('jquery_ui_version'),
166       '#description' => $this->t('Version of jQuery UI to load.'),
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_cdn.settings')
176       ->set('cdn', $form_state->getValue('cdn'))
177       ->set('jquery', $form_state->getValue('jquery_active'))
178       ->set('jquery_version', $form_state->getValue('jquery_version'))
179       ->set('jquery_ui_css', $form_state->getValue('jquery_ui_css'))
180       ->set('jquery_ui_js', $form_state->getValue('jquery_ui_js'))
181       ->set('jquery_ui_theme', $form_state->getValue('jquery_ui_theme'))
182       ->set('jquery_ui_version', $form_state->getValue('jquery_ui_version'))
183       ->set('minified', $form_state->getValue('minified'))
184       ->save();
185
186     parent::submitForm($form, $form_state);
187
188     // Clear relevant caches.
189     $this->cssCollectionOptimizer->deleteAll();
190     $this->jsCollectionOptimizer->deleteAll();
191     Cache::invalidateTags(['library_info']);
192   }
193
194 }