forked from backdrop-contrib/colorbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorbox.module
540 lines (487 loc) · 17.1 KB
/
colorbox.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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
<?php
/**
* @file
* A light-weight, customizable lightbox plugin for jQuery 1.3.
*/
/**
* Implements hook_config_info().
*/
function colorbox_config_info() {
$prefixes['colorbox.settings'] = array(
'label' => t('Colorbox settings'),
'group' => t('Configuration'),
);
return $prefixes;
}
/**
* Implements hook_autoload_info().
*/
function colorbox_autoload_info() {
return array(
'colorbox_handler_field_colorbox' => 'views/colorbox_handler_field_colorbox.inc',
);
}
/**
* Implements hook_theme().
*/
function colorbox_theme() {
return array(
'colorbox_imagefield' => array(
'variables' => array(
'image' => array(),
'path' => NULL,
'title' => NULL,
'gid' => NULL,
),
'file' => 'colorbox.theme.inc',
),
'colorbox_insert_image' => array(
'variables' => array(
'item' => NULL,
'widget' => NULL,
),
'template' => 'colorbox-insert-image',
'pattern' => 'colorbox_insert_image__[a-z0-9_]+',
'file' => 'colorbox.theme.inc',
),
'colorbox_image_formatter' => array(
'variables' => array(
'item' => NULL,
'entity_type' => NULL,
'entity' => NULL,
// Left for legacy support.
'node' => NULL,
'field' => array(),
'display_settings' => array(),
'delta' => NULL,
),
'file' => 'colorbox.theme.inc',
),
);
}
/**
* Implements hook_init().
*/
function colorbox_init() {
// Do not load colorbox during the Backdrop installation process, e.g. if part
// of installation profiles. Only add the JavaScript and CSS on specified
// paths.
if (!backdrop_installation_attempted() && _colorbox_active()) {
_colorbox_doheader();
}
}
/**
* Implements hook_views_api().
*/
function colorbox_views_api() {
return array(
'api' => 3,
'path' => backdrop_get_path('module', 'colorbox') . '/views',
);
}
/**
* Implements hook_library_info().
*/
function colorbox_library_info() {
$libraries['colorbox'] = array(
'title' => 'Colorbox',
'website' => 'http://www.jacklmoore.com/colorbox',
'version' => '1.6.4',
'js' => array(
backdrop_get_path('module', 'colorbox') . '/libraries/colorbox/jquery.colorbox-min.js' => array(),
),
);
$libraries['colorbox-source'] = array(
'title' => 'Colorbox',
'website' => 'http://www.jacklmoore.com/colorbox',
'version' => '1.6.4',
'js' => array(
backdrop_get_path('module', 'colorbox') . '/libraries/colorbox/jquery.colorbox.js' => array(),
),
);
return $libraries;
}
/**
* Implements hook_menu().
*/
function colorbox_menu() {
$items = array();
$items['admin/config/media/colorbox'] = array(
'title' => 'Colorbox',
'description' => 'Adjust Colorbox settings.',
'file' => 'colorbox.admin.inc',
'page callback' => 'backdrop_get_form',
'page arguments' => array('colorbox_admin_settings'),
'access arguments' => array('administer site configuration'),
);
return $items;
}
/**
* Check if Colorbox should be active for the current URL.
*
* @return bool
* TRUE if Colorbox should be active for the current page.
*/
function _colorbox_active() {
// Make it possible deactivate Colorbox with
// parameter ?colorbox=no in the url.
if (isset($_GET['colorbox']) && $_GET['colorbox'] == 'no') {
return FALSE;
}
// Code from the block_list function in block.module.
$path = backdrop_get_path_alias($_GET['q']);
$colorbox_pages = config_get('colorbox.settings', 'colorbox_pages');
// Compare with the internal and path alias (if any).
$page_match = backdrop_match_path($path, $colorbox_pages);
if ($path != $_GET['q']) {
$page_match = $page_match || backdrop_match_path($_GET['q'], $colorbox_pages);
}
$page_match = config_get('colorbox.settings', 'colorbox_visibility') == 0 ? !$page_match : $page_match;
return $page_match;
}
/**
* Loads the various js and css files.
*/
function _colorbox_doheader() {
static $already_added = FALSE;
if ($already_added) {
// Don't add the JavaScript and CSS multiple times.
return;
}
$config = config('colorbox.settings');
// Insert options and translated strings as JavaScript settings.
if ($config->get('colorbox_custom_settings_activate')) {
$js_settings = array(
'transition' => $config->get('colorbox_transition_type'),
'speed' => $config->get('colorbox_transition_speed'),
'opacity' => $config->get('colorbox_opacity'),
'slideshow' => $config->get('colorbox_slideshow') ? TRUE : FALSE,
'slideshowAuto' => $config->get('colorbox_slideshowauto') ? TRUE : FALSE,
'slideshowSpeed' => $config->get('colorbox_slideshowspeed'),
'slideshowStart' => $config->get('colorbox_text_start'),
'slideshowStop' => $config->get('colorbox_text_stop'),
'current' => strip_tags($config->get('colorbox_text_current')),
'previous' => strip_tags($config->get('colorbox_text_previous')),
'next' => strip_tags($config->get('colorbox_text_next')),
'close' => strip_tags($config->get('colorbox_text_close')),
'overlayClose' => $config->get('colorbox_overlayclose') ? TRUE : FALSE,
'returnFocus' => $config->get('colorbox_returnfocus') ? TRUE : FALSE,
'maxWidth' => $config->get('colorbox_maxwidth'),
'maxHeight' => $config->get('colorbox_maxheight'),
'initialWidth' => $config->get('colorbox_initialwidth'),
'initialHeight' => $config->get('colorbox_initialheight'),
'fixed' => $config->get('colorbox_fixed') ? TRUE : FALSE,
'scrolling' => $config->get('colorbox_scrolling') ? TRUE : FALSE,
'mobiledetect' => $config->get('colorbox_mobile_detect') ? TRUE : FALSE,
'mobiledevicewidth' => $config->get('colorbox_mobile_device_width'),
);
}
else {
$js_settings = array(
'opacity' => '0.85',
'current' => t('{current} of {total}'),
'previous' => t('« Prev'),
'next' => t('Next »'),
'close' => t('Close'),
'maxWidth' => '98%',
'maxHeight' => '98%',
'fixed' => TRUE,
'mobiledetect' => config_get('colorbox.settings', 'colorbox_mobile_detect') ? TRUE : FALSE,
'mobiledevicewidth' => config_get('colorbox.settings', 'colorbox_mobile_device_width'),
);
}
// Determine URL path to public files.
$js_settings['file_public_path'] = base_path() .
variable_get('file_public_path', conf_path() . '/files');
$js_settings['specificPagesDefaultValue'] = "admin*\nimagebrowser*\nimg_assist*\nimce*\nnode/add/*\nnode/*/edit\nprint/*\nprintpdf/*\nsystem/ajax\nsystem/ajax/*";
$path = backdrop_get_path('module', 'colorbox');
$style = config_get('colorbox.settings', 'colorbox_style');
// Give other modules the possibility to override Colorbox settings and style.
$data = &$js_settings;
backdrop_alter('colorbox_settings', $data, $style);
backdrop_add_js(array('colorbox' => $js_settings), array('type' => 'setting', 'scope' => JS_DEFAULT));
// Add and initialise the Colorbox plugin.
$variant = config_get('colorbox.settings', 'colorbox_compression_type');
if ($variant == 'minified') {
backdrop_add_library('colorbox', 'colorbox');
}
elseif ($variant == 'source') {
backdrop_add_library('colorbox', 'colorbox-source');
}
backdrop_add_js($path . '/js/colorbox.js');
// Add JS and CSS based on selected style.
switch ($style) {
case 'none':
break;
case 'default':
case 'plain':
case 'stockholmsyndrome':
backdrop_add_css($path . '/styles/' . $style . '/colorbox_style.css');
backdrop_add_js($path . '/styles/' . $style . '/colorbox_style.js');
break;
default:
backdrop_add_css($style . '/colorbox.css');
}
if (config_get('colorbox.settings', 'colorbox_load')) {
backdrop_add_js($path . '/js/colorbox_load.js');
}
if (config_get('colorbox.settings', 'colorbox_inline')) {
backdrop_add_js($path . '/js/colorbox_inline.js');
}
$already_added = TRUE;
}
/**
* Implements hook_field_formatter_info().
*/
function colorbox_field_formatter_info() {
return array(
'colorbox' => array(
'label' => t('Colorbox'),
'field types' => array('image'),
'settings' => array(
'colorbox_node_style' => '',
'colorbox_node_style_first' => '',
'colorbox_image_style' => '',
'colorbox_gallery' => 'post',
'colorbox_gallery_custom' => '',
'colorbox_caption' => 'auto',
'colorbox_caption_custom' => '',
'colorbox_multivalue_index' => NULL,
),
),
);
}
/**
* Implements hook_field_formatter_settings_form().
*/
function colorbox_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$image_styles = image_style_options(FALSE);
$image_styles_hide = $image_styles;
$image_styles_hide['hide'] = t('Hide (do not display image)');
$element['colorbox_node_style'] = array(
'#title' => t('Content image style'),
'#type' => 'select',
'#default_value' => $settings['colorbox_node_style'],
'#empty_option' => t('None (original image)'),
'#options' => $image_styles_hide,
'#description' => t('Image style to use in the content.'),
);
$element['colorbox_node_style_first'] = array(
'#title' => t('Content image style for first image'),
'#type' => 'select',
'#default_value' => $settings['colorbox_node_style_first'],
'#empty_option' => t('None (original image)'),
'#options' => $image_styles,
'#description' => t('Image style to use in the content for the first image.'),
);
$element['colorbox_image_style'] = array(
'#title' => t('Colorbox image style'),
'#type' => 'select',
'#default_value' => $settings['colorbox_image_style'],
'#empty_option' => t('None (original image)'),
'#options' => $image_styles,
'#description' => t('Image style to use in the Colorbox.'),
);
$gallery = array(
'post' => t('Per post gallery'),
'page' => t('Per page gallery'),
'field_post' => t('Per field in post gallery'),
'field_page' => t('Per field in page gallery'),
'custom' => t('Custom'),
'none' => t('No gallery'),
);
$element['colorbox_gallery'] = array(
'#title' => t('Gallery (image grouping)'),
'#type' => 'select',
'#default_value' => $settings['colorbox_gallery'],
'#options' => $gallery,
'#description' => t('How Colorbox should group the image galleries.'),
);
$element['colorbox_gallery_custom'] = array(
'#title' => t('Custom gallery'),
'#type' => 'textfield',
'#maxlength' => 32,
'#default_value' => $settings['colorbox_gallery_custom'],
'#description' => t('All images on a page with the same gallery value (rel attribute) will be grouped together. It must only contain lowercase letters, numbers, hyphen and underscores.'),
'#element_validate' => array('colorbox_gallery_custom_validate'),
'#required' => FALSE,
'#states' => array(
'visible' => array(
':input[name$="[settings_edit_form][settings][colorbox_gallery]"]' => array('value' => 'custom'),
),
),
);
$caption = array(
'auto' => t('Automatic'),
'title' => t('Title text'),
'alt' => t('Alt text'),
'node_title' => t('Content title'),
'custom' => t('Custom (with tokens)'),
'none' => t('None'),
);
$element['colorbox_caption'] = array(
'#title' => t('Caption'),
'#type' => 'select',
'#default_value' => $settings['colorbox_caption'],
'#options' => $caption,
'#description' => t('Automatic will use the first non-empty value of the title, the alt text and the content title.'),
);
$element['colorbox_caption_custom'] = array(
'#title' => t('Custom caption'),
'#type' => 'textfield',
'#default_value' => $settings['colorbox_caption_custom'],
'#states' => array(
'visible' => array(
':input[name$="[settings_edit_form][settings][colorbox_caption]"]' => array('value' => 'custom'),
),
),
);
// Allow users to hide or set a custom recursion limit.
// The module token_tweaks sets a global recursion limit that can not be
// bypassed.
if ($recursion_limit = min(config_get('colorbox.settings', 'token_tree_recursion_limit'), config_get('colorbox.settings', 'colorbox_token_recursion_limit'))) {
// File entities do not have $field, only $instance.
if (!empty($field['bundles'])) {
$token_types = array_merge(array_keys($field['bundles']), array('file'));
}
else {
$token_types = array($instance['entity_type'], 'file');
}
$element['colorbox_token'] = array(
'#type' => 'fieldset',
'#title' => t('Replacement patterns'),
'#theme' => 'token_tree',
'#token_types' => $token_types,
'#recursion_limit' => $recursion_limit,
'#dialog' => TRUE,
'#states' => array(
'visible' => array(
':input[name$="[settings_edit_form][settings][colorbox_caption]"]' => array('value' => 'custom'),
),
),
);
}
return $element;
}
/**
* Validate function for colorbox_gallery_custom.
*/
function colorbox_gallery_custom_validate($element, &$form_state) {
if (!empty($element['#value']) && !preg_match('!^[a-z0-9_-]+$!', $element['#value'])) {
form_error($element, t('%name must only contain lowercase letters, numbers, hyphen and underscores.', array('%name' => $element['#title'])));
}
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function colorbox_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = array();
$image_styles = image_style_options(FALSE);
// Unset possible 'No defined styles' option.
unset($image_styles['']);
// Styles could be lost because of enabled/disabled modules that defines
// their styles in code.
if (isset($image_styles[$settings['colorbox_node_style']])) {
$summary[] = t('Content image style: @style', array('@style' => $image_styles[$settings['colorbox_node_style']]));
}
elseif ($settings['colorbox_node_style'] == 'hide') {
$summary[] = t('Content image style: Hide');
}
else {
$summary[] = t('Content image style: Original image');
}
if (isset($image_styles[$settings['colorbox_node_style_first']])) {
$summary[] = t('Content image style of first image: @style', array('@style' => $image_styles[$settings['colorbox_node_style_first']]));
}
if (isset($image_styles[$settings['colorbox_image_style']])) {
$summary[] = t('Colorbox image style: @style', array('@style' => $image_styles[$settings['colorbox_image_style']]));
}
else {
$summary[] = t('Colorbox image style: Original image');
}
$gallery = array(
'post' => t('Per post gallery'),
'page' => t('Per page gallery'),
'field_post' => t('Per field in post gallery'),
'field_page' => t('Per field in page gallery'),
'custom' => t('Custom'),
'none' => t('No gallery'),
);
if (isset($settings['colorbox_gallery'])) {
$summary[] = t('Colorbox gallery type: @type', array('@type' => $gallery[$settings['colorbox_gallery']])) . ($settings['colorbox_gallery'] == 'custom' ? ' (' . $settings['colorbox_gallery_custom'] . ')' : '');
}
$caption = array(
'auto' => t('Automatic'),
'title' => t('Title text'),
'alt' => t('Alt text'),
'node_title' => t('Content title'),
'custom' => t('Custom (with tokens)'),
'none' => t('None'),
);
if (isset($settings['colorbox_caption'])) {
$summary[] = t('Colorbox caption: @type', array('@type' => $caption[$settings['colorbox_caption']]));
}
return implode('<br />', $summary);
}
/**
* Implements hook_field_formatter_view().
*/
function colorbox_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
$index = $display['settings']['colorbox_multivalue_index'];
foreach ($items as $delta => $item) {
if ($index === NULL || $index === $delta) {
$element[$delta] = array(
'#theme' => 'colorbox_image_formatter',
'#item' => $item,
'#entity_type' => $entity_type,
'#entity' => $entity,
// Left for legacy support.
'#node' => $entity,
'#field' => $field,
'#display_settings' => $display['settings'],
'#delta' => $delta,
);
}
}
return $element;
}
/**
* Implements hook_insert_styles().
*
* @todo Does the Backdrop version of Insert actually ship with this hook?
*/
function colorbox_insert_styles() {
$insert_styles = array();
foreach (image_styles() as $key => $style) {
$label = isset($style['label']) ? $style['label'] : $style['name'];
$insert_styles['colorbox__' . $key] = array('label' => t('Colorbox @style', array('@style' => $label)));
}
return $insert_styles;
}
/**
* Implements hook_insert_content().
*/
function colorbox_insert_content($item, $style, $widget) {
list($item['module_name'], $item['style_name']) = explode('__', $style['name'], 2);
return theme(array('colorbox_insert_image__' . str_replace('-', '_', $item['style_name']), 'colorbox_insert_image'), array(
'item' => $item,
'widget' => $widget,
));
}
/**
* Helper function.
*
* Machine names normally need to be unique but that does not apply to
* galleries.
*
* @return bool
* Always FALSE.
*/
function colorbox_gallery_exists() {
return FALSE;
}