3 namespace Drupal\advagg_ext_minify\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_ext_minify 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_ext_minify_settings';
69 protected function getEditableConfigNames() {
70 return ['advagg_ext_minify.settings'];
76 public function buildForm(array $form, FormStateInterface $form_state) {
80 $this->generateForm($form, ['css', $this->t('CSS')]);
83 $this->generateForm($form, ['js', $this->t('JavaScript')]);
84 return parent::buildForm($form, $form_state);
88 * Generate a form for css or js depending on the input.
91 * The form array to add to.
92 * @param array $params
94 * key 0 is the machine name
98 private function generateForm(array &$form, array $params) {
100 '#type' => 'fieldset',
101 '#title' => $this->t('@title', ['@title' => $params[1]]),
103 $form[$params[0]]['cmd'] = [
104 '#type' => 'fieldset',
105 '#title' => $this->t('Command Line'),
108 $description = $this->t('{%CWD%} = \Drupal::root(). <br /> {%IN%} = input file. <br /> {%IN_URL_ENC%} = url pointing to the input file that has been url encoded. <br /> {%OUT%} = output file. <br /><br />');
109 if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
110 $description .= ' ' . $this->t('Example using the <a href="@link1">Microsoft Ajax Minifier</a>. <p><code>@code1</code></p>', [
111 '@link1' => 'http://ajaxmin.codeplex.com/',
112 '@code1' => 'AjaxMinifier {%IN%} -o {%OUT%}',
116 if ($params[0] === 'js') {
117 $description .= ' ' . $this->t('Example using the <a href="@link1">Google Closure Compiler</a>. <p><code>@code1</code></p>', [
118 '@link1' => 'https://developers.google.com/closure/compiler/docs/gettingstarted_app',
119 '@code1' => 'java -jar compiler.jar --js {%CWD%}/{%IN%} --js_output_file {%OUT%}',
122 $description .= ' ' . $this->t('Example using curl to minify via the <a href="@link1">Online Google Closure Compiler</a>. <p><code>@code1</code></p>', [
123 '@link1' => 'https://developers.google.com/closure/compiler/docs/api-ref',
124 '@code1' => 'curl -o {%OUT%} -d output_info=compiled_code -d code_url={%IN_URL_ENC%} http://closure-compiler.appspot.com/compile',
127 if ($params[0] === 'css') {
128 $description .= ' ' . $this->t('Example using the <a href="@link1">YUI Compressor</a>. <p><code>@code1</code></p>', [
129 '@link1' => 'http://yui.github.io/yuicompressor/',
130 '@code1' => 'java -jar yuicompressor-x.y.z.jar --type css --line-break 4096 {%CWD%}/{%IN%} -o {%OUT%}',
133 $description .= ' ' . $this->t('Example using curl to minify via an online <a href="@link1">CSS Minifier</a>. <p><code>@code1</code></p>', [
134 '@link1' => 'http://cnvyr.io/',
135 '@code1' => 'curl -o {%OUT%} -F \'files0=@{%IN%}\' http://srv.cnvyr.io/v1?min=css',
139 $form[$params[0]]['cmd'][$params[0] . '_cmd'] = [
140 '#type' => 'textfield',
141 '#title' => $this->t('Command to run'),
142 '#default_value' => $this->config('advagg_ext_minify.settings')->get($params[0] . '_cmd'),
143 '#description' => $description,
151 public function submitForm(array &$form, FormStateInterface $form_state) {
152 $this->config('advagg_ext_minify.settings')
153 ->set('js_cmd', $form_state->getValue('js_cmd'))
154 ->set('css_cmd', $form_state->getValue('css_cmd'))
156 parent::submitForm($form, $form_state);
158 // Clear relevant caches.
159 $this->cssCollectionOptimizer->deleteAll();
160 $this->jsCollectionOptimizer->deleteAll();
161 Cache::invalidateTags([