Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / image_widget_crop / image_widget_crop.module
1 <?php
2
3 /**
4  * @file
5  * Contains image_widget_crop.module.
6  */
7
8 define('IMAGE_WIDGET_CROP_JS_CDN', 'https://cdnjs.cloudflare.com/ajax/libs/cropper/3.1.3/cropper.min.js');
9 define('IMAGE_WIDGET_CROP_CSS_CDN', 'https://cdnjs.cloudflare.com/ajax/libs/cropper/3.1.3/cropper.min.css');
10
11 use Drupal\Core\Entity\EntityInterface;
12 use Drupal\Core\Form\FormStateInterface;
13 use Drupal\Core\Routing\RouteMatchInterface;
14
15 /**
16  * Implements hook_help().
17  */
18 function image_widget_crop_help($route_name, RouteMatchInterface $route_match) {
19   switch ($route_name) {
20     case 'help.page.image_widget_crop':
21       $output = '';
22       $output .= '<h3>' . t('About') . '</h3>';
23       $output .= '<p>' . t('Implement CROP API into the fields image');
24       $output .= '<h3>' . t('Try module') . '</h3>';
25       $output .= '<p>' . t('You can Test ImageWidgetCrop in action directly with the sub-module, "ImageWidgetCrop example" to test differents usecase of this module');
26       $output .= '</dl>';
27       return $output;
28   }
29 }
30
31 /**
32  * Implements hook_field_widget_info_alter().
33  */
34 function image_widget_cropfield_widget_info_alter(array &$info) {
35   // Let a new field type re-use an existing widget.
36   $info['image_image']['field_types'][] = 'image_widget_crop';
37 }
38
39 /**
40  * Implements hook_libraries_info().
41  */
42 function image_widget_crop_libraries_info() {
43   $libraries = [
44     'cropper' => [
45       'name' => 'cropper',
46       'vendor url' => 'https://github.com/fengyuanchen/cropper',
47       'download url' => 'https://cdnjs.com/libraries/cropper',
48       'version arguments' => [
49         'file' => 'cropper.min.js',
50         'pattern' => '/Cropper v(.*)/',
51         'lines' => 2,
52       ],
53       'files' => [
54         'js' => [
55           'cropper.min.js' => [],
56         ],
57         'css' => [
58           'cropper.min.css' => [],
59         ],
60       ],
61     ],
62   ];
63   return $libraries;
64 }
65
66 /**
67  * Implements hook_library_info_alter().
68  */
69 function image_widget_crop_library_info_alter(&$libraries, $extension) {
70   if ($extension != 'image_widget_crop') {
71     return;
72   }
73
74   $config = \Drupal::config('image_widget_crop.settings');
75   $js = $config->get('settings.library_url');
76   $css = $config->get('settings.css_url');
77   // Explicit configuration takes priority.
78   if (!empty($js) && !empty($css)) {
79     $files = ['js' => $js, 'css' => $css];
80     foreach ($files as $type => $file_path) {
81       // Evaluate if the path are an local or external.
82       $is_local = parse_url($file_path, PHP_URL_SCHEME) === NULL && strpos($file_path, '//') !== 0;
83       // In that location $file_path are placed on root of module.
84       // not in drupal root.
85       $data = ($is_local && substr($file_path, 0, 1) !== '/') ? '/' . $file_path : $file_path;
86       if ($type === 'js') {
87         $libraries['cropper'][$type][$data] = [
88           'type' => $is_local ? 'file' : 'external',
89           'minified' => TRUE,
90         ];
91       }
92       else {
93         $libraries['cropper'][$type]['component'][$data] = [
94           'type' => $is_local ? 'file' : 'external',
95           'minified' => TRUE,
96         ];
97       }
98     }
99   }
100   // If Libraries exist and cropper library is available via Libraries API.
101   elseif (\Drupal::moduleHandler()->moduleExists('libraries')
102     && ($info = libraries_detect('cropper'))
103     && $info['installed']) {
104     $libraries['cropper']['version'] = $info['version'];
105     foreach ($info['files'] as $type => $files) {
106       // Fetch all possible entry.
107       foreach ($files as $data => $option) {
108         if (is_numeric($data)) {
109           $option = "/{$info['library path']}/{$option}";
110         }
111         elseif (empty($option['type']) || $option['type'] == 'file') {
112           $data = "/{$info['library path']}/{$data}";
113         }
114       }
115
116       if ($type == 'css') {
117         $libraries['cropper']['css']['theme'][$data] = $option;
118       }
119       else {
120         $libraries['cropper'][$type][$data] = $option;
121         $libraries['cropper'][$type][$data] = $option;
122       }
123     }
124   }
125   else {
126     // Fallback to CDN.
127     $js = IMAGE_WIDGET_CROP_JS_CDN;
128     $libraries['cropper']['js'][$js] = [
129       'type' => 'external',
130       'minified' => TRUE,
131     ];
132     $css = IMAGE_WIDGET_CROP_CSS_CDN;
133     $libraries['cropper']['css']['component'][$css] = [
134       'type' => 'external',
135       'minified' => TRUE,
136     ];
137     $libraries['cropper']['version'] = 'web-hosted';
138   }
139 }
140
141 /**
142  * Implements hook_form_FORM_ID_alter().
143  */
144 function image_widget_crop_form_file_form_alter(&$form, FormStateInterface $form_state, $form_id) {
145   /** @var \Drupal\file_entity\Entity\FileEntity $file */
146   $file = $form_state->getFormObject()->getEntity();
147   $mime_type = $file->getMimeTypeType();
148   $operation = $form_state->getFormObject()->getOperation();
149   $crop_config = \Drupal::config('image_widget_crop.settings');
150   if ($mime_type == 'image' && ($operation == 'edit' || $operation == 'inline_edit')) {
151     $form['image_crop'] = [
152       '#type' => 'image_crop',
153       '#file' => $file,
154       '#crop_type_list' => $crop_config->get('settings.crop_list'),
155       '#crop_preview_image_style' => $crop_config->get('settings.crop_preview_image_style'),
156       '#show_default_crop' => $crop_config->get('settings.show_default_crop'),
157       '#warn_mupltiple_usages' => $crop_config->get('settings.warn_mupltiple_usages'),
158     ];
159     $form['actions']['submit']['#submit'][] = 'image_widget_crop_form_submit';
160   }
161 }
162
163 /**
164  * Implements hook_entity_extra_field_info().
165  */
166 function image_widget_crop_entity_extra_field_info() {
167   $return = [];
168   $return['file']['image']['form']['crop_preview_wrapper'] = [
169     'label' => t('Crop image'),
170     'weight' => 10,
171   ];
172
173   return $return;
174 }
175
176 /**
177  * Implements hook_entity_insert().
178  */
179 function image_widget_crop_entity_insert(EntityInterface $entity) {
180   \Drupal::service('image_widget_crop.manager')->buildCropToEntity($entity);
181 }
182
183 /**
184  * Implements hook_entity_update().
185  */
186 function image_widget_crop_entity_update(EntityInterface $entity) {
187   \Drupal::service('image_widget_crop.manager')->buildCropToEntity($entity);
188 }
189
190 /**
191  * Form submission handler for image_widget_crop_form_file_form_alter.
192  *
193  * @param array $form
194  *   The complete form array.
195  * @param \Drupal\Core\Form\FormStateInterface $form_state
196  *   The current state of the form.
197  */
198 function image_widget_crop_form_submit(array &$form, FormStateInterface $form_state) {
199   \Drupal::service('image_widget_crop.manager')->buildCropToForm($form_state);
200 }
201
202 /**
203  * Implements hook_filefield_sources_widgets().
204  */
205 function image_widget_crop_filefield_sources_widgets() {
206   return ['image_widget_crop'];
207 }
208
209 /**
210  * Implements hook_imce_supported_widgets_alter().
211  */
212 function image_widget_crop_imce_supported_widgets_alter(array &$widgets) {
213   $widgets[] = 'image_widget_crop';
214   return $widgets;
215 }