Pathologic was missing because of a .git folder inside.
[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 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Entity\FieldableEntityInterface;
10 use Drupal\Core\Entity\EntityInterface;
11 use Drupal\Core\Routing\RouteMatchInterface;
12 use Drupal\crop\Entity\Crop;
13 use Drupal\crop\Entity\CropType;
14 use Drupal\file\Entity\File;
15 use Drupal\file\Plugin\Field\FieldType\FileFieldItemList;
16
17 /**
18  * Implements hook_help().
19  */
20 function image_widget_crop_help($route_name, RouteMatchInterface $route_match) {
21   switch ($route_name) {
22     case 'help.page.image_widget_crop':
23       $output = '';
24       $output .= '<h3>' . t('About') . '</h3>';
25       $output .= '<p>' . t('Implement CROP API into the fields image');
26       $output .= '<h3>' . t('Try module') . '</h3>';
27       $output .= '<p>' . t('You can Test ImageWidgetCrop in action directly with the sub-module, "ImageWidgetCrop example" to test differents usecase of this module');
28       $output .= '</dl>';
29       return $output;
30   }
31 }
32
33 /**
34  * Implements hook_field_widget_info_alter().
35  */
36 function image_widget_cropfield_widget_info_alter(array &$info) {
37   // Let a new field type re-use an existing widget.
38   $info['image_image']['field_types'][] = 'image_widget_crop';
39 }
40
41 /**
42  * Implements hook_libraries_info().
43  */
44 function image_widget_crop_libraries_info() {
45   $libraries = [
46     'cropper' => [
47       'name' => 'cropper',
48       'vendor url' => 'https://github.com/fengyuanchen/cropper',
49       'download url' => 'https://cdnjs.com/libraries/cropper',
50       'version arguments' => [
51         'file' => 'cropper.min.js',
52         'pattern' => '/Cropper v(.*)/',
53         'lines' => 2,
54       ],
55       'files' => [
56         'js' => [
57           'cropper.min.js' => [],
58         ],
59         'css' => [
60           'cropper.min.css' => [],
61         ],
62       ],
63     ],
64   ];
65   return $libraries;
66 }
67
68 /**
69  * Implements hook_library_info_alter().
70  */
71 function image_widget_crop_library_info_alter(&$libraries, $extension) {
72   if ($extension != 'image_widget_crop') {
73     return;
74   }
75
76   $config = \Drupal::config('image_widget_crop.settings');
77   $js  = $config->get('settings.library_url');
78   $css = $config->get('settings.css_url');
79   // Explicit configuration takes priority.
80   if (!empty($js) && !empty($css)) {
81     $files = ['js' => $js, 'css' =>  $css];
82     foreach ($files as $type => $file_path) {
83       // Evaluate if the path are an local or external.
84       $is_local = parse_url($file_path, PHP_URL_SCHEME) === NULL && strpos($file_path, '//') !== 0;
85       // In that location $file_path are placed on root of module not in drupal root.
86       $data = ($is_local && substr($file_path, 0, 1) !== '/') ? '/' . $file_path : $file_path;
87       if ($type === 'js') {
88         $libraries['cropper'][$type][$data] = [
89           'type' => $is_local ? 'file' : 'external',
90           'minified' => TRUE,
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  = 'https://cdnjs.cloudflare.com/ajax/libs/cropper/2.3.4/cropper.min.js';
128     $libraries['cropper']['js'][$js] = [
129       'type' => 'external',
130       'minified' => TRUE,
131     ];
132     $css = 'https://cdnjs.cloudflare.com/ajax/libs/cropper/2.3.4/cropper.min.css';
133     $libraries['cropper']['css']['component'][$css] = [
134       'type' => 'external',
135       'minified' => TRUE,
136     ];
137     $libraries['cropper']['version'] = 'web-hosted';
138     $libraries['cropper.integration']['js'] = [];
139   }
140 }
141
142 /**
143  * Implements hook_form_alter().
144  */
145 function image_widget_crop_form_file_form_alter(&$form, FormStateInterface $form_state, $form_id) {
146   /** @var \Drupal\file_entity\Entity\FileEntity $file */
147   $file = $form_state->getFormObject()->getEntity();
148   $mime_type = $file->getMimeTypeType();
149   $operation = $form_state->getFormObject()->getOperation();
150   $crop_config = \Drupal::config('image_widget_crop.settings');
151   if ($mime_type == 'image' && ($operation == 'edit' || $operation == 'inline_edit')) {
152     $form['image_crop'] = [
153       '#type' => 'image_crop',
154       '#file' => $file,
155       '#crop_type_list' => $crop_config->get('settings.crop_list'),
156       '#crop_preview_image_style' => $crop_config->get('settings.crop_preview_image_style'),
157       '#show_default_crop' => $crop_config->get('settings.show_default_crop'),
158       '#warn_mupltiple_usages' => $crop_config->get('settings.warn_mupltiple_usages'),
159     ];
160     $form['actions']['submit']['#submit'][] = 'image_widget_crop_form_submit';
161   }
162 }
163
164 /**
165  * Implements hook_entity_extra_field_info().
166  */
167 function image_widget_crop_entity_extra_field_info() {
168   $return = [];
169   $return['file']['image']['form']['crop_preview_wrapper'] = [
170     'label' => t('Crop image'),
171     'weight' => 10,
172   ];
173
174   return $return;
175 }
176
177 /**
178  * Implements hook_entity_insert().
179  */
180 function image_widget_crop_entity_insert(EntityInterface $entity) {
181   \Drupal::service('image_widget_crop.manager')->buildCropToEntity($entity);
182 }
183
184 /**
185  * Implements hook_entity_update().
186  */
187 function image_widget_crop_entity_update(EntityInterface $entity) {
188   \Drupal::service('image_widget_crop.manager')->buildCropToEntity($entity);
189 }
190
191 /**
192  * Form submission handler for image_widget_crop_form_file_form_alter.
193  *
194  * @param array $form
195  *   The complete form array.
196  * @param \Drupal\Core\Form\FormStateInterface $form_state
197  *   The current state of the form.
198  */
199 function image_widget_crop_form_submit(array &$form, FormStateInterface $form_state) {
200   \Drupal::service('image_widget_crop.manager')->buildCropToForm($form_state);
201 }
202
203 /**
204  * Implements hook_filefield_sources_widgets().
205  */
206 function image_widget_crop_filefield_sources_widgets() {
207   return ['image_widget_crop'];
208 }
209
210 /**
211  * Implements hook_imce_supported_widgets_alter().
212  */
213 function image_widget_crop_imce_supported_widgets_alter(array &$widgets) {
214   $widgets[] = 'image_widget_crop';
215   return $widgets;
216 }