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