3 namespace Drupal\image_widget_crop\Form;
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;
14 * Configure ImageWidgetCrop general settings for this site.
16 class CropWidgetForm extends ConfigFormBase {
19 * The settings of image_widget_crop configuration.
23 * @see \Drupal\Core\Config\Config
28 * Instance of API ImageWidgetCropManager.
30 * @var \Drupal\image_widget_crop\ImageWidgetCropManager
32 protected $imageWidgetCropManager;
35 * Constructs a CropWidgetForm object.
37 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
38 * The factory for configuration objects.
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;
49 public static function create(ContainerInterface $container) {
51 $container->get('config.factory'),
52 $container->get('image_widget_crop.manager')
59 public function getFormId() {
60 return 'image_widget_crop_settings_form';
66 protected function getEditableConfigNames() {
67 return ['image_widget_crop.settings'];
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';
80 '#title' => $this->t('Cropper library settings'),
81 '#description' => $this->t('Changes here require a cache rebuild to take full effect.'),
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>.', [
91 '#default_value' => $this->settings->get('settings.library_url'),
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>.', [
101 '#default_value' => $this->settings->get('settings.css_url'),
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']);
111 $form['library']['library_url']['#attributes']['placeholder'] = $cdn_js;
112 $form['library']['css_url']['#attributes']['placeholder'] = $cdn_css;
115 $form['image_crop'] = [
116 '#type' => 'details',
117 '#title' => t('General configuration'),
120 $form['image_crop']['crop_preview_image_style'] = [
121 '#title' => $this->t('Crop preview image style'),
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.'),
129 $form['image_crop']['crop_list'] = [
130 '#title' => $this->t('Crop Type'),
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'),
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'),
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'),
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'),
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'),
158 return parent::buildForm($form, $form_state);
162 * Validation for cropper library.
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.
169 public function validateForm(array &$form, FormStateInterface $form_state) {
170 parent::validateForm($form, $form_state);
172 if (!empty($form_state->getValue('library_url')) || !empty($form_state->getValue('css_url'))) {
174 'library' => $form_state->getValue('library_url'),
175 'css' => $form_state->getValue('css_url'),
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.'));
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.'));
187 elseif (!$is_local) {
189 $result = \Drupal::httpClient()->request('GET', $file);
190 if ($result->getStatusCode() != 200) {
191 throw new \Exception($result->getReasonPhrase(), 1);
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(),
208 public function submitForm(array &$form, FormStateInterface $form_state) {
209 parent::submitForm($form, $form_state);
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']);
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();