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