-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathimage_widget_crop.module
More file actions
272 lines (241 loc) · 8.29 KB
/
Copy pathimage_widget_crop.module
File metadata and controls
272 lines (241 loc) · 8.29 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
/**
* @file
* Contains image_widget_crop.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Render\Element;
use Drupal\image_widget_crop\ImageWidgetCrop;
use Drupal\file\Plugin\Field\FieldType\FileFieldItemList;
/**
* 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 .= '</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_theme().
*/
function image_widget_crop_theme() {
return [
'crop_container' => [
'render element' => 'element',
],
'crop_image_container' => [
'render element' => 'element',
],
'crop_sidebar' => [
'render element' => 'element',
],
'crop_list_items' => [
'render element' => 'element'
]
];
}
/**
* Prepares variables for crop_list_items templates.
*
* Default template: crop-list-items.html.twig.
*
* @param array $variables
* An associative array containing:
* - element: An associative array containing the properties of the element.
* Properties used: #id, #attributes, #children.
*/
function template_preprocess_crop_list_items(array &$variables) {
$variables['has_parent'] = FALSE;
$element = $variables['element'];
// Ensure #attributes is set.
$element += array('#attributes' => array());
// Special handling for form elements.
if (isset($element['#array_parents'])) {
// Assign an html ID.
if (!isset($element['#attributes']['id'])) {
$element['#attributes']['id'] = $element['#id'];
}
$variables['has_parent'] = TRUE;
}
// Get variables into elements and pass it to twig template.
foreach ($element['#variables'] as $variable_key => $var) {
$variables[$variable_key] = $var;
}
$variables['attributes'] = $element['#attributes'];
}
/**
* Prepares variables for crop_container templates.
*
* Default template: crop-container.html.twig.
*
* @param array $variables
* An associative array containing:
* - element: An associative array containing the properties of the element.
* Properties used: #id, #attributes, #children.
*/
function template_preprocess_crop_container(array &$variables) {
$variables['has_parent'] = FALSE;
$element = $variables['element'];
// Ensure #attributes is set.
$element += array('#attributes' => array());
// Special handling for form elements.
if (isset($element['#array_parents'])) {
// Assign an html ID.
if (!isset($element['#attributes']['id'])) {
$element['#attributes']['id'] = $element['#id'];
}
$variables['has_parent'] = TRUE;
}
// Determine whether there are any child elements in the element that are not
// fully-specified render arrays. If there are any, then the child
// elements present nested lists and we automatically inherit the render
// array properties of the current list to them.
foreach (Element::children($element) as $key) {
$items[] = $element[$key];
foreach ($items as $child_key => $child_value) {
if (!isset($items['#type']) && !isset($items['#theme']) && !isset($items['#markup'])) {
if (!isset($items['#items'])) {
$items['#items'][$child_key] = $child_value;
}
}
}
}
$variables['items'] = $items;
$variables['attributes'] = $element['#attributes'];
$variables['element_type'] = $element['#element_type'];
}
/**
* Prepares variables for crop_sidebar templates.
*
* Default template: crop-sidebar.html.twig.
*
* @param array $variables
* An associative array containing:
* - element: An associative array containing the properties of the element.
* Properties used: #id, #attributes, #children.
*/
function template_preprocess_crop_sidebar(array &$variables) {
$variables['has_parent'] = FALSE;
$element = $variables['element'];
// Ensure #attributes is set.
$element += array('#attributes' => array());
// Special handling for form elements.
if (isset($element['#array_parents'])) {
// Assign an html ID.
if (!isset($element['#attributes']['id'])) {
$element['#attributes']['id'] = $element['#id'];
}
$variables['has_parent'] = TRUE;
}
// Get all child elements into sidebar.
foreach (Element::children($element) as $key) {
$child = &$element[$key];
$items[] = $child;
}
// Get the type of wrapper element.
$variables['element_type'] = $element['#element_type'];
// Add attributes to wrapper element.
$variables['attributes'] = $element['#attributes'];
// Send to twig all items into sidebar element.
$variables['items'] = $items;
}
/**
* Prepares variables for container templates.
*
* Default template: crop-sidebar.html.twig.
*
* @param array $variables
* An associative array containing:
* - element: An associative array containing the properties of the element.
* Properties used: #id, #attributes, #children.
*/
function template_preprocess_crop_image_container(array &$variables) {
$variables['has_parent'] = FALSE;
$element = $variables['element'];
// Ensure #attributes is set.
$element += array('#attributes' => array());
// Special handling for form elements.
if (isset($element['#array_parents'])) {
// Assign an html ID.
if (!isset($element['#attributes']['id'])) {
$element['#attributes']['id'] = $element['#id'];
}
$variables['has_parent'] = TRUE;
}
foreach ($element['#variables'] as $variable_key => $var) {
$variables[$variable_key] = $var;
}
foreach (Element::children($element) as $key) {
$child = &$element[$key];
$items[] = $child;
}
// Send to twig all items into sidebar element.
$variables['items'] = $items;
$variables['attributes'] = $element['#attributes'];
}
/**
* Implements hook_entity_presave().
*/
function image_widget_crop_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {
if (isset($entity) && method_exists($entity, 'getFields')) {
foreach ($entity->getFields() as $entity_fields) {
if ($entity_fields instanceof FileFieldItemList && isset($entity_fields->crop_preview_wrapper)) {
// Set edit state to False by default.
$edit = FALSE;
// Get the route params attributes.
$route_params = \Drupal::requestStack()
->getCurrentRequest()->attributes->get('_route_params');
/** @var \Drupal\image_widget_crop\ImageWidgetCrop $crop */
$crop = new ImageWidgetCrop();
// Verify if the current form is an edit.
if (isset($route_params['_entity_form']) && preg_match('/.edit/', $route_params['_entity_form'])) {
$edit = TRUE;
}
// Parse the value of a crop_preview_wrapper element and get,
// the properties associate with her image style.
foreach ($entity_fields->crop_preview_wrapper['container'] as $image_style_name => $properties) {
/** @var \Drupal\image\Entity\ImageStyle $image_style */
$image_style = \Drupal::entityManager()
->getStorage('image_style')
->load($image_style_name);
// Save the entity if has not already been saved by some other code.
if (isset($properties['values']) && (!empty($properties['values']['crop-w']) && !empty($properties['values']['crop-h']))) {
$crop->cropByImageStyle($properties['values'], $entity_fields->getValue()['0'], $image_style, $edit);
}
elseif ($properties['delete-crop'] == '1') {
$crop->deleteCrop($entity_fields->getValue()['0']['file-uri'], $image_style);
}
}
}
}
}
}
/**
* Implements hook_css_alter().
*/
function image_widget_crop_css_alter(&$css) {
// Add FontAwesome.
$fontawesome = '//netdna.bootstrapcdn.com/font-awesome/4.0.1/css/font-awesome.min.css';
$css[$fontawesome] = array(
'data' => $fontawesome,
'type' => 'external',
'every_page' => TRUE,
'media' => 'all',
'preprocess' => FALSE,
'group' => CSS_AGGREGATE_THEME,
'browsers' => array('IE' => TRUE, '!IE' => TRUE),
'weight' => -2,
);
}