-
Notifications
You must be signed in to change notification settings - Fork 1
/
picture.admin.inc
291 lines (268 loc) · 11.8 KB
/
picture.admin.inc
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
/**
* @file
* Picture - map breakpoints and image styles.
*/
/**
* Select breakpoint group form.
*/
function picture_select_breakpoint_group_form($form, &$form_state) {
$groups = breakpoints_breakpoint_group_load_all();
$options = array();
foreach ($groups as $group) {
$options[$group->machine_name] = $group->name;
}
if ($options) {
$form['breakpoint_group'] = array(
'#title' => t('Breakpoint group'),
'#type' => 'select',
'#required' => TRUE,
'#description' => t('Select a breakpoint group.'),
'#options' => $options,
'#default_value' => $form_state['item']->getBreakpointGroup() ? $form_state['item']->getBreakpointGroup()->machine_name : NULL,
);
}
else {
$form['breakpoint_group'] = array(
'#title' => t('Breakpoint group'),
'#type' => 'item',
'#markup' => t(
'There are no breakpoint groups. !create_link.',
array('!create_link' => l(t('Create a breakpoint group'), 'admin/config/media/breakpoints/groups/add'))
),
);
}
return $form;
}
/**
* Validate breakpoint group.
*/
function picture_select_breakpoint_group_form_validate($form, &$form_state) {
if (!isset($form_state['values']['breakpoint_group']) || empty($form_state['values']['breakpoint_group'])) {
form_error($form['breakpoint_group'], t('No breakpoint group was selected.'));
}
}
/**
* Submit callback.
*/
function picture_select_breakpoint_group_form_submit($form, &$form_state) {
$form_state['item']->setBreakpointGroup($form_state['values']['breakpoint_group']);
}
/**
* Validate callback.
*/
function picture_mapping_form_validate($form, &$form_state) {
$picture_mapping = $form_state['item'];
if ($form_state['triggering_element']['#type'] != 'submit') {
$picture_mapping->setBreakpointGroup($form_state['values']['breakpoint_group']);
$form_state['rebuild'] = TRUE;
return;
}
$picture_mapping->setLabel($form_state['values']['label']);
$picture_mapping->setMachineName($form_state['values']['machine_name']);
$picture_mapping->setMappings($form_state['values']['mapping']);
// Make sure at least one mapping is defined.
if (!$picture_mapping->hasMappings()) {
form_error($form['mapping'], t('Please select at least one mapping.'));
}
}
/**
* Chooses which picture groups are available in the CKEditor image dialog.
*/
function picture_admin_settings() {
$config = config('picture.settings');
$form = array();
$picture_mappings = picture_get_mapping_options();
// Check if picture mappings have been configured before proceeding.
if ($picture_mappings) {
// Create a settings form.
$form['description'] = array(
'#type' => 'item',
'#title' => t('Choose which picture mappings will be available in the CKEditor image dialog.'),
'#description' => t('See picture_wysiwyg.css for an example of how to style these images in your theme using the selectors suggested below.'),
);
// Retrieve pre-existing settings.
$ckeditor_mappings = $config->get('ckeditor_mappings');
// Make option list for CKEditor default mapping.
$ckeditor_default_mapping_options = array('not_set' => 'Not Set');
// Loop through each picture mapping and place a checkbox and weight.
foreach ($picture_mappings as $machine_name => $display_name) {
// Add option to CKEditor default mapping if enabled.
if (isset($ckeditor_mappings[$machine_name]) && $ckeditor_mappings[$machine_name]['enabled']) {
$ckeditor_default_mapping_options[$machine_name] = $display_name;
}
$form[$machine_name] = array(
'#type' => 'fieldset',
'#title' => t('@name picture mapping', array('@name' => $display_name)),
);
$form[$machine_name]['enabled'] = array(
'#type' => 'checkbox',
'#default_value' => isset($ckeditor_mappings[$machine_name]) ? $ckeditor_mappings[$machine_name]['enabled'] : 0,
'#title' => t('Include @name picture mapping in the CKEditor image dialog', array('@name' => $display_name)),
);
$form[$machine_name]['css'] = array(
'#type' => 'item',
'#markup' => t('Consider using the selector <code>span[data-picture-mapping="@machine_name"]</code> in your theme CSS.', array(
'@machine_name' => $machine_name,
)),
);
$form[$machine_name]['weight'] = array(
'#type' => 'select',
'#title' => t('Weight'),
'#options' => backdrop_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)),
'#default_value' => isset($ckeditor_mappings[$machine_name]) ? $ckeditor_mappings[$machine_name]['weight'] : 1,
'#description' => t('Control the sort order of picture mappings in the CKEditor "size" drop-down. Higher weights sink to the bottom of the list.'),
);
$form[$machine_name]['fallback'] = array(
'#type' => 'select',
'#title' => t('Fallback image style'),
'#options' => backdrop_map_assoc(array_keys(image_styles())) + array(
PICTURE_EMPTY_IMAGE => t('Empty image'),
),
'#default_value' => isset($ckeditor_mappings[$machine_name]) ? $ckeditor_mappings[$machine_name]['fallback'] : NULL,
);
$form[$machine_name]['lazyload'] = array(
'#title' => t('Picture lazyload'),
'#description' => t('Image will be rendered when it appears in viewport, helps to optimize page load speed.'),
'#type' => 'checkbox',
'#default_value' => isset($ckeditor_mappings[$machine_name]['lazyload']) ? $ckeditor_mappings[$machine_name]['lazyload'] : 0,
);
$form[$machine_name]['lazyload_aspect_ratio'] = array(
'#title' => t('Keep aspect ratio'),
'#type' => 'checkbox',
'#description' => t('Preserve the space for the image being lazyloaded to avoid layout reflows. <br /> Image ratio is defined per breakpoint, make sure all images from srcset have the same ratio. <br />Output example: !example',
array('!example' => htmlentities('<source media="(...)" data-srcset="image_400x200.jpg x1, image_800x400.jpg x2, image_1200x600.jpg x3" data-aspectratio="2" />'))
),
'#default_value' => isset($ckeditor_mappings[$machine_name]['lazyload_aspect_ratio']) ? $ckeditor_mappings[$machine_name]['lazyload_aspect_ratio'] : 0,
'#states' => array(
'visible' => array(
':input[name="article_responsive_image[lazyload]"]' => array('checked' => TRUE),
':input[name="article_responsive_image[fallback]"]' => array('value' => PICTURE_EMPTY_IMAGE),
),
),
);
}
$form['#tree'] = TRUE;
$form['ckeditor_label'] = array(
'#type' => 'textfield',
'#title' => t('Label in the CKEditor image dialog'),
'#description' => t('This sets the label for the drop-down select box containing these picture mappings in the CKEditor image dialog'),
'#default_value' => $config->get('ckeditor_label'),
);
$form['ckeditor_default_mapping'] = array(
'#type' => 'select',
'#title' => t('Default CKEditor Picture Mapping'),
'#options' => $ckeditor_default_mapping_options,
'#default_value' => $config->get('ckeditor_default_mapping'),
'#description' => t('This sets the default picture mapping in CKEditor'),
);
}
else {
$form['info'] = array(
'#markup' => t('No picture mappings found. !create', array('!create' => l(t('Create one now.'), 'admin/config/media/picture/add'))),
);
}
$form['js_scope'] = array(
'#title' => t('Picture JavaScript scope'),
'#description' => t("For performance reasons, JS should be loaded in the footer. However, you can move the Picture module's JS to the header so that it loads before JS from other modules like Views Slideshow."),
'#type' => 'radios',
'#options' => array(
'header' => t('Header'),
'footer' => t('Footer'),
),
'#default_value' => $config->get('js_scope'),
);
$form['polyfill_version'] = array(
'#title' => t('Picture polyfill to use'),
'#description' => t('For performance reasons, use the minified version.'),
'#type' => 'radios',
'#options' => array(
'min' => t('Minified polyfill'),
'dev' => t('Development polyfill'),
'own' => t('Use custom polyfill'),
),
'#default_value' => $config->get('polyfill_version'),
);
$form['fallback_method'] = array(
'#title' => t('Picture fallback method'),
'#description' => t('Use the fallback method, the default is to use srcset since it avoids double downloads, but src is the right method and will also work if the browser does not support javascript.'),
'#type' => 'radios',
'#options' => array(
'src' => t('src fallback'),
'srcset' => t('srcset fallback'),
),
'#default_value' => $config->get('fallback_method'),
);
$form['img_sizes_output_method'] = array(
'#title' => t('"Image with sizes" output method'),
'#description' => t('"srcset on Image tag" creates the correct markup, however "picture element" will prevent duplicate downloads.'),
'#type' => 'radios',
'#options' => array(
'img_srcset' => t('srcset on Image tag'),
'picture_element' => t('Picture element'),
),
'#default_value' => $config->get('img_sizes_output_method'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Save',
);
return $form;
}
/**
* Validate handler for the picture_ckeditor_settings form.
*
* It checks that a fallback image style is selected for every
* picture mapping that has been enabled for the CKEditor image dialog.
*/
function picture_admin_settings_validate($form, &$form_state) {
$picture_mappings = picture_mapping_load_all();
if ($picture_mappings) {
foreach ($picture_mappings as $picture_mapping) {
$machine_name = $picture_mapping->getMachineName();
if ($form_state['values'][$machine_name]['enabled'] == 1) {
if (empty($form_state['values'][$machine_name]['fallback'])) {
form_set_error($machine_name . '][fallback', t('Please choose a fallback image style for this picture mapping'));
}
}
}
}
}
/**
* Submit handler, places chosen picture groups into the variables table.
*/
function picture_admin_settings_submit($form, &$form_state) {
$config = config('picture.settings');
$picture_mappings = picture_mapping_load_all();
$ckeditor_mappings = array();
if ($picture_mappings) {
// Loop each picture mapping and record the settings.
foreach ($picture_mappings as $picture_mapping) {
$machine_name = $picture_mapping->getMachineName();
$ckeditor_mappings[$machine_name]['enabled'] = $form_state['values'][$machine_name]['enabled'];
$ckeditor_mappings[$machine_name]['weight'] = $form_state['values'][$machine_name]['weight'];
$ckeditor_mappings[$machine_name]['fallback'] = $form_state['values'][$machine_name]['fallback'];
$ckeditor_mappings[$machine_name]['lazyload'] = $form_state['values'][$machine_name]['lazyload'];
$ckeditor_mappings[$machine_name]['lazyload_aspect_ratio'] = $form_state['values'][$machine_name]['lazyload_aspect_ratio'];
}
uasort($ckeditor_mappings, 'picture_compare_weights');
$config->set('ckeditor_mappings', $ckeditor_mappings);
$config->set('ckeditor_label', $form_state['values']['ckeditor_label']);
$config->set('ckeditor_default_mapping', $form_state['values']['ckeditor_default_mapping']);
}
$config->set('js_scope', $form_state['values']['js_scope']);
$config->set('polyfill_version', $form_state['values']['polyfill_version']);
$config->set('fallback_method', $form_state['values']['fallback_method']);
$config->set('img_sizes_output_method', $form_state['values']['img_sizes_output_method']);
$config->save();
backdrop_set_message(t('Your settings have been saved'));
}
/**
* Sort picture groups for the CKEditor image dialog.
*/
function picture_compare_weights($a, $b) {
if ($a['weight'] == $b['weight']) {
return 0;
}
return ($a['weight'] < $b['weight']) ? -1 : 1;
}