5 * Contains image_widget_crop.module.
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;
18 * Implements hook_help().
20 function image_widget_crop_help($route_name, RouteMatchInterface $route_match) {
21 switch ($route_name) {
22 case 'help.page.image_widget_crop':
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');
34 * Implements hook_field_widget_info_alter().
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';
42 * Implements hook_libraries_info().
44 function image_widget_crop_libraries_info() {
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(.*)/',
57 'cropper.min.js' => [],
60 'cropper.min.css' => [],
69 * Implements hook_library_info_alter().
71 function image_widget_crop_library_info_alter(&$libraries, $extension) {
72 if ($extension != 'image_widget_crop') {
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;
88 $libraries['cropper'][$type][$data] = [
89 'type' => $is_local ? 'file' : 'external',
93 $libraries['cropper'][$type]['component'][$data] = [
94 'type' => $is_local ? 'file' : 'external',
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}";
111 elseif (empty($option['type']) || $option['type'] == 'file') {
112 $data = "/{$info['library path']}/{$data}";
116 if ($type == 'css') {
117 $libraries['cropper']['css']['theme'][$data] = $option;
120 $libraries['cropper'][$type][$data] = $option;
121 $libraries['cropper'][$type][$data] = $option;
127 $js = 'https://cdnjs.cloudflare.com/ajax/libs/cropper/2.3.4/cropper.min.js';
128 $libraries['cropper']['js'][$js] = [
129 'type' => 'external',
132 $css = 'https://cdnjs.cloudflare.com/ajax/libs/cropper/2.3.4/cropper.min.css';
133 $libraries['cropper']['css']['component'][$css] = [
134 'type' => 'external',
137 $libraries['cropper']['version'] = 'web-hosted';
138 $libraries['cropper.integration']['js'] = [];
143 * Implements hook_form_alter().
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',
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'),
160 $form['actions']['submit']['#submit'][] = 'image_widget_crop_form_submit';
165 * Implements hook_entity_extra_field_info().
167 function image_widget_crop_entity_extra_field_info() {
169 $return['file']['image']['form']['crop_preview_wrapper'] = [
170 'label' => t('Crop image'),
178 * Implements hook_entity_insert().
180 function image_widget_crop_entity_insert(EntityInterface $entity) {
181 \Drupal::service('image_widget_crop.manager')->buildCropToEntity($entity);
185 * Implements hook_entity_update().
187 function image_widget_crop_entity_update(EntityInterface $entity) {
188 \Drupal::service('image_widget_crop.manager')->buildCropToEntity($entity);
192 * Form submission handler for image_widget_crop_form_file_form_alter.
195 * The complete form array.
196 * @param \Drupal\Core\Form\FormStateInterface $form_state
197 * The current state of the form.
199 function image_widget_crop_form_submit(array &$form, FormStateInterface $form_state) {
200 \Drupal::service('image_widget_crop.manager')->buildCropToForm($form_state);
204 * Implements hook_filefield_sources_widgets().
206 function image_widget_crop_filefield_sources_widgets() {
207 return ['image_widget_crop'];
211 * Implements hook_imce_supported_widgets_alter().
213 function image_widget_crop_imce_supported_widgets_alter(array &$widgets) {
214 $widgets[] = 'image_widget_crop';