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\ImageWidgetCropInterface;
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 ImageWidgetCropManager object.
30 * @var \Drupal\image_widget_crop\ImageWidgetCropInterface
32 protected $imageWidgetCropManager;
35 * Constructs a CropWidgetForm object.
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.
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;
51 public static function create(ContainerInterface $container) {
53 $container->get('config.factory'),
54 $container->get('image_widget_crop.manager')
61 public function getFormId() {
62 return 'image_widget_crop_settings_form';
68 protected function getEditableConfigNames() {
69 return ['image_widget_crop.settings'];
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';
82 '#title' => $this->t('Cropper library settings'),
83 '#description' => $this->t('Changes here require a cache rebuild to take full effect.'),
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>.', [
93 '#default_value' => $this->settings->get('settings.library_url'),
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>.', [
103 '#default_value' => $this->settings->get('settings.css_url'),
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']);
113 $form['library']['library_url']['#attributes']['placeholder'] = $cdn_js;
114 $form['library']['css_url']['#attributes']['placeholder'] = $cdn_css;
117 $form['image_crop'] = [
118 '#type' => 'details',
119 '#title' => t('General configuration'),
122 $form['image_crop']['crop_preview_image_style'] = [
123 '#title' => $this->t('Crop preview image style'),
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.'),
131 $form['image_crop']['crop_list'] = [
132 '#title' => $this->t('Crop Type'),
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'),
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'),
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'),
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'),
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'),
160 return parent::buildForm($form, $form_state);
164 * Validation for cropper library.
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.
171 public function validateForm(array &$form, FormStateInterface $form_state) {
172 parent::validateForm($form, $form_state);
174 if (!empty($form_state->getValue('library_url')) || !empty($form_state->getValue('css_url'))) {
176 'library' => $form_state->getValue('library_url'),
177 'css' => $form_state->getValue('css_url'),
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.'));
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.'));
189 elseif (!$is_local) {
191 $result = \Drupal::httpClient()->request('GET', $file);
192 if ($result->getStatusCode() != 200) {
193 throw new \Exception($result->getReasonPhrase(), 1);
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(),
210 public function submitForm(array &$form, FormStateInterface $form_state) {
211 parent::submitForm($form, $form_state);
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']);
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();