colorbox.module 15.1 KB
Newer Older
jdwfly's avatar
jdwfly committed
1
<?php
2

jdwfly's avatar
jdwfly committed
3
4
5
6
/**
 * @file
 * A light-weight, customizable lightbox plugin for jQuery 1.3
 */
7
8
9
10

/**
 * The default path to the Colorbox directory.
 */
11
define('COLORBOX_MIN_PLUGIN_VERSION', '1.3.20');
12
13
14


/**
15
 * Implements hook_theme().
16
17
 */
function colorbox_theme() {
18
  return array(
19
    'colorbox_imagefield' => array(
20
      'variables' => array(
21
        'image' => array(),
22
23
24
25
        'path' => NULL,
        'title' => NULL,
        'gid' => NULL,
      ),
26
27
28
      'file' => 'colorbox.theme.inc',
    ),

29
30
31
32
33
34
35
36
37
    'colorbox_insert_image' => array(
      'variables' => array(
        'item' => NULL,
        'widget' => NULL,
      ),
      'template' => 'colorbox-insert-image',
      'file' => 'colorbox.theme.inc',
    ),

38
39
40
    'colorbox_image_formatter' => array(
      'variables' => array(
        'item' => NULL,
41
42
43
        'entity_type' => NULL,
        'entity' => NULL,
        'node' => NULL,  // Left for legacy support.
44
        'field' => array(),
45
        'display_settings' => array(),
46
47
48
49
      ),
      'file' => 'colorbox.theme.inc',
    ),
  );
50
51
52
}

/**
53
 * Implements hook_init().
54
55
 */
function colorbox_init() {
56
57
58
59
60
  // Do not load colorbox during the Drupal installation process, e.g. if part
  // of installation profiles.
  if (!drupal_installation_attempted()) {
    _colorbox_doheader();
  }
61
62
}

63
/**
64
 * Implements hook_views_api().
65
66
67
68
 */
function colorbox_views_api() {
  return array(
    'api' => 2,
69
    'path' => drupal_get_path('module', 'colorbox') . '/views',
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
/**
 * Implements hook_libraries_info().
 */
function colorbox_libraries_info() {
  $libraries['colorbox'] = array(
    'name' => 'Colorbox plugin',
    'vendor url' => 'http://www.jacklmoore.com/colorbox',
    'download url' => 'http://www.jacklmoore.com/colorbox',
    'path' => 'colorbox',
    'version arguments' => array(
      'file' => 'colorbox/jquery.colorbox-min.js',
      'pattern' => '@ColorBox v([0-9\.a-z]+)@',
      'lines' => 1,
    ),
    'files' => array(
      'js' => array(
        'jquery.colorbox-min.js',
      ),
    ),
    'variants' => array(
      'minified' => array(
        'files' => array(
          'js' => array(
            'jquery.colorbox-min.js',
          ),
        ),
      ),
      'source' => array(
        'files' => array(
          'js' => array(
            'jquery.colorbox.js',
          ),
        ),
      ),
    ),
  );

  return $libraries;
}

113
/**
114
 * Implements hook_menu().
115
 */
116
function colorbox_menu() {
117
118
  $items = array();

119
  $items['admin/config/media/colorbox'] = array(
120
    'title' => 'Colorbox',
121
    'description' => 'Adjust Colorbox settings.',
122
    'file' => 'colorbox.admin.inc',
123
124
125
126
    'page callback' => 'drupal_get_form',
    'page arguments' => array('colorbox_admin_settings'),
    'access arguments' => array('administer site configuration'),
  );
127

128
129
130
  return $items;
}

131
132
133
134
135
136
137
/**
 * Check if Colorbox should be active for the current URL.
 *
 * @return
 *   TRUE if Colorbox should be active for the current page.
 */
function _colorbox_active() {
138
139
140
141
142
143
  // Make it possible deactivate Colorbox with
  // parameter ?colorbox=no in the url.
  if (isset($_GET['colorbox']) && $_GET['colorbox'] == 'no') {
    return FALSE;
  }

144
145
  // Code from the block_list funtion in block.module.
  $path = drupal_get_path_alias($_GET['q']);
146
  $colorbox_pages = variable_get('colorbox_pages', "admin*\nimagebrowser*\nimg_assist*\nimce*\nnode/add/*\nnode/*/edit\nprint/*\nprintpdf/*\nsystem/ajax\nsystem/ajax/*");
147
  // Compare with the internal and path alias (if any).
148
  $page_match = drupal_match_path($path, $colorbox_pages);
149
  if ($path != $_GET['q']) {
150
    $page_match = $page_match || drupal_match_path($_GET['q'], $colorbox_pages);
151
  }
152
  $page_match = variable_get('colorbox_visibility', 0) == 0 ? !$page_match : $page_match;
153

154
  return $page_match;
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
}

/**
 * Loads the various js and css files.
 */
function _colorbox_doheader() {
  static $already_added = FALSE;
  if ($already_added) {
    return; // Don't add the JavaScript and CSS multiple times.
  }
  if (!_colorbox_active()) {
    return; // Don't add the JavaScript and CSS on specified paths.
  }

  // Insert options and translated strings as javascript settings.
  if (variable_get('colorbox_custom_settings_activate', 0)) {
    $js_settings = array(
      'transition' => variable_get('colorbox_transition_type', 'elastic'),
      'speed' => variable_get('colorbox_transition_speed', 350),
174
      'opacity' => variable_get('colorbox_opacity', '0.85'),
175
      'slideshow' => variable_get('colorbox_slideshow', 0) ? TRUE : FALSE,
176
177
178
179
      'slideshowAuto' => variable_get('colorbox_slideshowauto', 1) ? TRUE : FALSE,
      'slideshowSpeed' => variable_get('colorbox_slideshowspeed', 2500),
      'slideshowStart' => variable_get('colorbox_text_start', 'start slideshow'),
      'slideshowStop' => variable_get('colorbox_text_stop', 'stop slideshow'),
180
181
182
183
      'current' => variable_get('colorbox_text_current', '{current} of {total}'),
      'previous' => variable_get('colorbox_text_previous', '« Prev'),
      'next' => variable_get('colorbox_text_next', 'Next »'),
      'close' => variable_get('colorbox_text_close', 'Close'),
184
      'overlayClose' => variable_get('colorbox_overlayclose', 1) ? TRUE : FALSE,
frjo's avatar
frjo committed
185
186
      'maxWidth' => variable_get('colorbox_maxwidth', '98%'),
      'maxHeight' => variable_get('colorbox_maxheight', '98%'),
187
      'initialWidth' => variable_get('colorbox_initialwidth', '300'),
frjo's avatar
frjo committed
188
      'initialHeight' => variable_get('colorbox_initialheight', '250'),
189
      'fixed' => variable_get('colorbox_fixed', 1) ? TRUE : FALSE,
190
      'scrolling' => variable_get('colorbox_scrolling', 1) ? TRUE : FALSE,
191
192
193
194
    );
  }
  else {
    $js_settings = array(
195
      'opacity' => '0.85',
196
197
198
199
      'current' => t('{current} of {total}'),
      'previous' => t('« Prev'),
      'next' => t('Next »'),
      'close' => t('Close'),
frjo's avatar
frjo committed
200
201
      'maxWidth' => '98%',
      'maxHeight' => '98%',
202
      'fixed' => TRUE,
203
    );
204
  }
205
206
207
208

  $path = drupal_get_path('module', 'colorbox');
  $style = variable_get('colorbox_style', 'default');

209
  // Give other modules the possibility to override Colorbox settings and style.
210
  $data = &$js_settings;
211
  drupal_alter('colorbox_settings', $data, $style);
212

213
  drupal_add_js(array('colorbox' => $js_settings), array('type' => 'setting', 'scope' => JS_DEFAULT));
214

215
  // Add and initialise the Colorbox plugin.
216
217
  $variant = variable_get('colorbox_compression_type', 'minified');
  libraries_load('colorbox', $variant);
218
  drupal_add_js($path . '/js/colorbox.js');
219

220
221
222
223
224
  // Add JS and CSS based on selected style.
  switch ($style) {
    case 'none':
      break;
    case 'default':
frjo's avatar
frjo committed
225
    case 'plain':
226
    case 'stockholmsyndrome':
frjo's avatar
frjo committed
227
228
      drupal_add_css($path . '/styles/' . $style . '/colorbox_style.css');
      drupal_add_js($path . '/styles/' . $style . '/colorbox_style.js');
229
230
231
      break;
    default:
      drupal_add_css($style . '/colorbox.css');
232
233
  }

234
  if (variable_get('colorbox_load', 0)) {
235
    drupal_add_js($path . '/js/colorbox_load.js');
236
237
  }

238
239
240
241
  if (variable_get('colorbox_inline', 0)) {
    drupal_add_js($path . '/js/colorbox_inline.js');
  }

242
  $already_added = TRUE;
243
244
}

jdwfly's avatar
jdwfly committed
245
/**
246
 * Implements hook_field_formatter_info().
jdwfly's avatar
jdwfly committed
247
248
 */
function colorbox_field_formatter_info() {
249
250
251
252
  return array(
    'colorbox' => array(
      'label' => t('Colorbox'),
      'field types' => array('image'),
253
254
255
256
257
258
      'settings' => array(
        'colorbox_node_style' => '',
        'colorbox_image_style' => '',
        'colorbox_gallery' => 'post',
        'colorbox_gallery_custom' => '',
        'colorbox_caption' => 'auto',
259
        'colorbox_caption_custom' => '',
260
      ),
261
262
    ),
  );
263
}
264

265
/**
266
 * Implements hook_field_formatter_settings_form().
267
268
269
270
271
272
 */
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);
273
274
  $image_styles_hide = $image_styles;
  $image_styles_hide['hide'] = t('Hide (do not display image)');
275
  $element['colorbox_node_style'] = array(
276
    '#title' => t('Content image style'),
277
278
279
    '#type' => 'select',
    '#default_value' => $settings['colorbox_node_style'],
    '#empty_option' => t('None (original image)'),
280
    '#options' => $image_styles_hide,
281
    '#description' => t('Image style to use in the content.'),
282
283
284
285
286
287
288
  );
  $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,
289
    '#description' => t('Image style to use in the Colorbox.'),
290
291
  );

292
293
294
295
296
297
298
299
300
301
302
303
304
  $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,
305
    '#description' => t('How Colorbox should group the image galleries.'),
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
  );
  $element['colorbox_gallery_custom'] = array(
    '#title' => t('Custom gallery'),
    '#type' => 'machine_name',
    '#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, and underscores.'),
    '#required' => FALSE,
    '#machine_name' => array(
      'exists' => 'colorbox_gallery_exists',
      'error' => t('The custom gallery field must only contain lowercase letters, numbers, and underscores.'),
    ),
    '#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'),
329
    'node_title' => t('Content title'),
330
    'custom' => t('Custom (with tokens)'),
331
332
333
334
335
336
337
    'none' => t('None'),
  );
  $element['colorbox_caption'] = array(
    '#title' => t('Caption'),
    '#type' => 'select',
    '#default_value' => $settings['colorbox_caption'],
    '#options' => $caption,
338
    '#description' => t('Automatic will use the first none empty value of the title, the alt text and the content title.'),
339
  );
340
341
342
343
344
345
346
347
348
349
  $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'),
      ),
    ),
  );
350
351
352
  // 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 (module_exists('token') && $recursion_limit = min(variable_get('token_tree_recursion_limit', 3), variable_get('colorbox_token_recursion_limit', 3))) {
353
354
355
356
    $element['colorbox_token'] = array(
      '#type' => 'fieldset',
      '#title' => t('Replacement patterns'),
      '#theme' => 'token_tree',
357
      '#token_types' => array($instance['entity_type'], 'file'),
358
      '#recursion_limit' => $recursion_limit,
359
      '#dialog' => TRUE,
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
      '#states' => array(
        'visible' => array(
          ':input[name$="[settings_edit_form][settings][colorbox_caption]"]' => array('value' => 'custom'),
        ),
      ),
    );
  }
  else {
    $element['colorbox_token'] = array(
      '#type' => 'fieldset',
      '#title' => t('Replacement patterns'),
      '#description' => '<strong class="error">' . t('For token support the <a href="@token_url">token module</a> must be installed.', array('@token_url' => 'http://drupal.org/project/token')) . '</strong>',
      '#states' => array(
        'visible' => array(
          ':input[name$="[settings_edit_form][settings][colorbox_caption]"]' => array('value' => 'custom'),
        ),
      ),
    );
  }
379

380
  return $element;
381
}
382

383
/**
384
 * Implements hook_field_formatter_settings_summary().
385
 */
386
387
388
389
390
391
392
393
394
395
396
397
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']])) {
398
    $summary[] = t('Content image style: @style', array('@style' => $image_styles[$settings['colorbox_node_style']]));
399
  }
400
  elseif ($settings['colorbox_node_style'] == 'hide') {
401
    $summary[] = t('Content image style: Hide');
402
  }
403
  else {
404
    $summary[] = t('Content image style: Original image');
405
  }
406

407
408
  if (isset($image_styles[$settings['colorbox_image_style']])) {
    $summary[] = t('Colorbox image style: @style', array('@style' => $image_styles[$settings['colorbox_image_style']]));
409
  }
410
411
412
413
  else {
    $summary[] = t('Colorbox image style: Original image');
  }

414
415
416
417
418
419
420
421
  $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'),
  );
422
423
424
  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'] . ')' : '');
  }
425
426
427
428
429

  $caption = array(
    'auto' =>  t('Automatic'),
    'title' => t('Title text'),
    'alt' => t('Alt text'),
430
    'node_title' => t('Content title'),
431
    'custom' => t('Custom (with tokens)'),
432
433
    'none' => t('None'),
  );
434
435
436
  if (isset($settings['colorbox_caption'])) {
    $summary[] = t('Colorbox caption: @type', array('@type' => $caption[$settings['colorbox_caption']]));
  }
437

438
439
440
441
  return implode('<br />', $summary);
}

/**
442
 * Implements hook_field_formatter_view().
443
444
445
 */
function colorbox_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
446
447
448
449
450

  foreach ($items as $delta => $item) {
    $element[$delta] = array(
      '#theme' => 'colorbox_image_formatter',
      '#item' => $item,
451
452
453
      '#entity_type' => $entity_type,
      '#entity' => $entity,
      '#node' => $entity, // Left for legacy support.
454
      '#field' => $field,
455
      '#display_settings' => $display['settings'],
456
457
458
459
460
461
    );
  }

  return $element;
}

462
/**
463
 * Implements hook_insert_styles().
464
465
466
 */
function colorbox_insert_styles() {
  $insert_styles = array();
467
  foreach (image_styles() as $key => $style) {
468
    $insert_styles['colorbox__' . $key] = array('label' => t('Colorbox @style', array('@style' => $style['name'])));
469
  }
470
471
472
473
474

  return $insert_styles;
}

/**
475
 * Implements hook_insert_content().
476
477
 */
function colorbox_insert_content($item, $style, $widget) {
478
  list($item['module_name'], $item['style_name']) = explode('__', $style['name'], 2);
479
  return theme('colorbox_insert_image', array('item' => $item, 'widget' => $widget));
480
}
481
482
483
484
485
486
487
488
489
490

/**
 * Machine names normally need to be unique but that does not apply to galleries.
 *
 * @return
 *   Always FALSE
 */
function colorbox_gallery_exists() {
  return FALSE;
}