6c613f6633b501f3f74e9d7b3b86e1b75c8ac7a4
[yaffs-website] / web / modules / contrib / image_widget_crop / src / Form / CropWidgetForm.php
1 <?php
2
3 namespace Drupal\image_widget_crop\Form;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Config\ConfigFactoryInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Form\ConfigFormBase;
9 use Drupal\crop\Entity\CropType;
10 use Drupal\image_widget_crop\ImageWidgetCropManager;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * Configure ImageWidgetCrop general settings for this site.
15  */
16 class CropWidgetForm extends ConfigFormBase {
17
18   /**
19    * The settings of image_widget_crop configuration.
20    *
21    * @var array
22    *
23    * @see \Drupal\Core\Config\Config
24    */
25   protected $settings;
26
27   /**
28    * Instance of API ImageWidgetCropManager.
29    *
30    * @var \Drupal\image_widget_crop\ImageWidgetCropManager
31    */
32   protected $imageWidgetCropManager;
33
34   /**
35    * Constructs a CropWidgetForm object.
36    *
37    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
38    *   The factory for configuration objects.
39    */
40   public function __construct(ConfigFactoryInterface $config_factory, ImageWidgetCropManager $iwc_manager) {
41     parent::__construct($config_factory);
42     $this->settings = $this->config('image_widget_crop.settings');
43     $this->imageWidgetCropManager = $iwc_manager;
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('image_widget_crop.manager')
53     );
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public function getFormId() {
60     return 'image_widget_crop_settings_form';
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   protected function getEditableConfigNames() {
67     return ['image_widget_crop.settings'];
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public function buildForm(array $form, FormStateInterface $form_state) {
74     $url = 'https://cdnjs.com/libraries/cropper';
75     $cdn_js = 'https://cdnjs.cloudflare.com/ajax/libs/cropper/2.3.4/cropper.min.js';
76     $cdn_css = 'https://cdnjs.cloudflare.com/ajax/libs/cropper/2.3.4/cropper.min.css';
77
78     $form['library'] = [
79       '#type' => 'details',
80       '#title' => $this->t('Cropper library settings'),
81       '#description' => $this->t('Changes here require a cache rebuild to take full effect.'),
82     ];
83
84     $form['library']['library_url'] = [
85       '#type' => 'textfield',
86       '#title' => $this->t('Custom Cropper library'),
87       '#description' => $this->t('Set the URL or local path for the file, or leave empty to use the installed library (if present) or a <a href="@file">CDN</a> fallback. You can retrieve the library file from <a href="@url">Cropper CDN</a>.', [
88         '@url' => $url,
89         '@file' => $cdn_js,
90       ]),
91       '#default_value' => $this->settings->get('settings.library_url'),
92     ];
93
94     $form['library']['css_url'] = [
95       '#type' => 'textfield',
96       '#title' => $this->t('Custom Cropper CSS file'),
97       '#description' => $this->t('Set the URL or local path for the file, or leave empty to use installed library (if installed) or a <a href="@file">CDN</a> fallback. You can retrieve the CSS file from <a href="@url">Cropper CDN</a>.', [
98         '@url' => $url,
99         '@file' => $cdn_css,
100       ]),
101       '#default_value' => $this->settings->get('settings.css_url'),
102     ];
103
104     // Indicate which files are used when custom urls are not set.
105     if (\Drupal::moduleHandler()->moduleExists('libraries')
106       && ($info = libraries_detect('cropper')) && $info['installed']) {
107       $form['library']['library_url']['#attributes']['placeholder'] = $info['library path'] . '/dist/' . key($info['files']['js']);
108       $form['library']['css_url']['#attributes']['placeholder'] = $info['library path'] . '/dist/' . key($info['files']['css']);
109     }
110     else {
111       $form['library']['library_url']['#attributes']['placeholder'] = $cdn_js;
112       $form['library']['css_url']['#attributes']['placeholder'] = $cdn_css;
113     }
114
115     $form['image_crop'] = [
116       '#type' => 'details',
117       '#title' => t('General configuration'),
118     ];
119
120     $form['image_crop']['crop_preview_image_style'] = [
121       '#title' => $this->t('Crop preview image style'),
122       '#type' => 'select',
123       '#options' => $this->imageWidgetCropManager->getAvailableCropImageStyle(image_style_options(FALSE)),
124       '#default_value' => $this->settings->get('settings.crop_preview_image_style'),
125       '#description' => $this->t('The preview image will be shown while editing the content.'),
126       '#weight' => 15,
127     ];
128
129     $form['image_crop']['crop_list'] = [
130       '#title' => $this->t('Crop Type'),
131       '#type' => 'select',
132       '#options' => $this->imageWidgetCropManager->getAvailableCropType(CropType::getCropTypeNames()),
133       '#empty_option' => $this->t('<@no-preview>', ['@no-preview' => $this->t('no preview')]),
134       '#default_value' => $this->settings->get('settings.crop_list'),
135       '#multiple' => TRUE,
136       '#description' => $this->t('The type of crop to apply to your image. If your Crop Type not appear here, set an image style use your Crop Type'),
137       '#weight' => 16,
138     ];
139
140     $form['image_crop']['show_crop_area'] = [
141       '#title' => $this->t('Always expand crop area'),
142       '#type' => 'checkbox',
143       '#default_value' => $this->settings->get('settings.show_crop_area'),
144     ];
145
146     $form['image_crop']['warn_multiple_usages'] = [
147       '#title' => $this->t('Warn user when a file have multiple usages'),
148       '#type' => 'checkbox',
149       '#default_value' => $this->settings->get('settings.warn_multiple_usages'),
150     ];
151
152     $form['image_crop']['show_default_crop'] = [
153       '#title' => $this->t('Show default crop area'),
154       '#type' => 'checkbox',
155       '#default_value' => $this->settings->get('settings.show_default_crop'),
156     ];
157
158     return parent::buildForm($form, $form_state);
159   }
160
161   /**
162    * Validation for cropper library.
163    *
164    * @param array $form
165    *   An associative array containing the structure of the form.
166    * @param \Drupal\Core\Form\FormStateInterface $form_state
167    *   The current state of the form.
168    */
169   public function validateForm(array &$form, FormStateInterface $form_state) {
170     parent::validateForm($form, $form_state);
171
172     if (!empty($form_state->getValue('library_url')) || !empty($form_state->getValue('css_url'))) {
173       $files = [
174         'library' => $form_state->getValue('library_url'),
175         'css' => $form_state->getValue('css_url'),
176       ];
177       if (empty($files['library']) || empty($files['css'])) {
178         $form_state->setErrorByName('plugin', t('Please provide both a library and a CSS file when using custom URLs.'));
179       }
180       else {
181         foreach ($files as $type => $file) {
182           // Verify that both files exist.
183           $is_local = parse_url($file, PHP_URL_SCHEME) === NULL && strpos($file, '//') !== 0;
184           if ($is_local && !file_exists($file)) {
185             $form_state->setErrorByName($type . '_url', t('The provided local file does not exist.'));
186           }
187           elseif (!$is_local) {
188             try {
189               $result = \Drupal::httpClient()->request('GET', $file);
190               if ($result->getStatusCode() != 200) {
191                 throw new \Exception($result->getReasonPhrase(), 1);
192               }
193             }
194             catch (\Exception $e) {
195               $form_state->setErrorByName($type . '_url', t('The remote URL for the library does not appear to be valid: @message.', [
196                 '@message' => $e->getMessage(),
197               ]), 'error');
198             }
199           }
200         }
201       }
202     }
203   }
204
205   /**
206    * {@inheritdoc}
207    */
208   public function submitForm(array &$form, FormStateInterface $form_state) {
209     parent::submitForm($form, $form_state);
210
211     // We need to rebuild the library cache if we switch from remote to local
212     // library or vice-versa.
213     Cache::invalidateTags(['library_info']);
214
215     $this->settings
216       ->set("settings.library_url", $form_state->getValue('library_url'))
217       ->set("settings.css_url", $form_state->getValue('css_url'))
218       ->set("settings.crop_preview_image_style", $form_state->getValue('crop_preview_image_style'))
219       ->set("settings.show_default_crop", $form_state->getValue('show_default_crop'))
220       ->set("settings.show_crop_area", $form_state->getValue('show_crop_area'))
221       ->set("settings.warn_multiple_usages", $form_state->getValue('warn_multiple_usages'))
222       ->set("settings.crop_list", $form_state->getValue('crop_list'));
223     $this->settings->save();
224   }
225
226 }