Projet

Général

Profil

Paste
Télécharger (17,3 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / colorbox / colorbox.module @ 651307cd

1
<?php
2

    
3
/**
4
 * @file
5
 * A light-weight, customizable lightbox plugin for jQuery 1.3
6
 */
7

    
8
/**
9
 * The minimum required version of the Colorbox plugin.
10
 */
11
define('COLORBOX_MIN_PLUGIN_VERSION', '1.6.1');
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
        'path' => NULL,
23
        'title' => NULL,
24
        'gid' => NULL,
25
      ),
26
      'file' => 'colorbox.theme.inc',
27
    ),
28

    
29
    'colorbox_insert_image' => array(
30
      'variables' => array(
31
        'item' => NULL,
32
        'widget' => NULL,
33
      ),
34
      'template' => 'colorbox-insert-image',
35
      'pattern' => 'colorbox_insert_image__[a-z0-9_]+',
36
      'file' => 'colorbox.theme.inc',
37
    ),
38

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

    
54
/**
55
 * Implements hook_init().
56
 */
57
function colorbox_init() {
58
  // Do not load colorbox during the Drupal installation process, e.g. if part
59
  // of installation profiles. Only add the JavaScript and CSS on specified paths.
60
  if (!drupal_installation_attempted() && _colorbox_active()) {
61
    _colorbox_doheader();
62
  }
63
}
64

    
65
/**
66
 * Implements hook_views_api().
67
 */
68
function colorbox_views_api() {
69
  return array(
70
    'api' => 2,
71
    'path' => drupal_get_path('module', 'colorbox') . '/views',
72
  );
73
}
74

    
75
/**
76
 * Implements hook_libraries_info().
77
 */
78
function colorbox_libraries_info() {
79
  $libraries['colorbox'] = array(
80
    'name' => 'Colorbox plugin',
81
    'vendor url' => 'http://www.jacklmoore.com/colorbox',
82
    'download url' => 'https://github.com/jackmoore/colorbox/archive/1.x.zip',
83
    'version arguments' => array(
84
      'file' => 'jquery.colorbox-min.js',
85
      'pattern' => '@(?i:Colorbox)\sv?([0-9\.a-z]+)@',
86
      'lines' => 5,
87
    ),
88
    'files' => array(
89
      'js' => array(
90
        'jquery.colorbox-min.js',
91
      ),
92
    ),
93
    'variants' => array(
94
      'minified' => array(
95
        'files' => array(
96
          'js' => array(
97
            'jquery.colorbox-min.js',
98
          ),
99
        ),
100
      ),
101
      'source' => array(
102
        'files' => array(
103
          'js' => array(
104
            'jquery.colorbox.js',
105
          ),
106
        ),
107
      ),
108
    ),
109
  );
110

    
111
  return $libraries;
112
}
113

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

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

    
129
  return $items;
130
}
131

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

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

    
155
  // Allow other modules to change the state of colorbox for the current URL.
156
  drupal_alter('colorbox_active', $page_match);
157

    
158
  return $page_match;
159
}
160

    
161
/**
162
 * Loads the various js and css files.
163
 */
164
function _colorbox_doheader() {
165
  static $already_added = FALSE;
166
  if ($already_added) {
167
    return; // Don't add the JavaScript and CSS multiple times.
168
  }
169

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

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

    
215
  // Give other modules the possibility to override Colorbox settings and style.
216
  $data = &$js_settings;
217
  drupal_alter('colorbox_settings', $data, $style);
218

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

    
221
  // Add and initialise the Colorbox plugin.
222
  $variant = variable_get('colorbox_compression_type', 'minified');
223
  if (module_exists('libraries')) {
224
    libraries_load('colorbox', $variant);
225
  }
226
  drupal_add_js($path . '/js/colorbox.js');
227

    
228
  // Add JS and CSS based on selected style.
229
  switch ($style) {
230
    case 'none':
231
      break;
232
    case 'default':
233
    case 'plain':
234
    case 'stockholmsyndrome':
235
      drupal_add_css($path . '/styles/' . $style . '/colorbox_style.css');
236
      drupal_add_js($path . '/styles/' . $style . '/colorbox_style.js');
237
      break;
238
    default:
239
      drupal_add_css($style . '/colorbox.css');
240
  }
241

    
242
  if (variable_get('colorbox_load', 0)) {
243
    drupal_add_js($path . '/js/colorbox_load.js');
244
  }
245

    
246
  if (variable_get('colorbox_inline', 0)) {
247
    drupal_add_js($path . '/js/colorbox_inline.js');
248
  }
249

    
250
  $already_added = TRUE;
251
}
252

    
253
/**
254
 * Implements hook_field_formatter_info().
255
 */
256
function colorbox_field_formatter_info() {
257
  return array(
258
    'colorbox' => array(
259
      'label' => t('Colorbox'),
260
      'field types' => array('image'),
261
      'settings' => array(
262
        'colorbox_node_style' => '',
263
        'colorbox_node_style_first' => '',
264
        'colorbox_image_style' => '',
265
        'colorbox_gallery' => 'post',
266
        'colorbox_gallery_custom' => '',
267
        'colorbox_caption' => 'auto',
268
        'colorbox_caption_custom' => '',
269
        'colorbox_multivalue_index' => NULL,
270
      ),
271
    ),
272
  );
273
}
274

    
275
/**
276
 * Implements hook_field_formatter_settings_form().
277
 */
278
function colorbox_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
279
  $display = $instance['display'][$view_mode];
280
  $settings = $display['settings'];
281

    
282
  $image_styles = image_style_options(FALSE);
283
  $image_styles_hide = $image_styles;
284
  $image_styles_hide['hide'] = t('Hide (do not display image)');
285
  $element['colorbox_node_style'] = array(
286
    '#title' => t('Content image style'),
287
    '#type' => 'select',
288
    '#default_value' => $settings['colorbox_node_style'],
289
    '#empty_option' => t('None (original image)'),
290
    '#options' => $image_styles_hide,
291
    '#description' => t('Image style to use in the content.'),
292
  );
293
  $element['colorbox_node_style_first'] = array(
294
    '#title' => t('Content image style for first image'),
295
    '#type' => 'select',
296
    '#default_value' => $settings['colorbox_node_style_first'],
297
    '#empty_option' => t('No special style.'),
298
    '#options' => $image_styles,
299
    '#description' => t('Image style to use in the content for the first image.'),
300
  );
301
  $element['colorbox_image_style'] = array(
302
    '#title' => t('Colorbox image style'),
303
    '#type' => 'select',
304
    '#default_value' => $settings['colorbox_image_style'],
305
    '#empty_option' => t('None (original image)'),
306
    '#options' => $image_styles,
307
    '#description' => t('Image style to use in the Colorbox.'),
308
  );
309

    
310
  $gallery = array(
311
    'post' => t('Per post gallery'),
312
    'page' => t('Per page gallery'),
313
    'field_post' => t('Per field in post gallery'),
314
    'field_page' => t('Per field in page gallery'),
315
    'custom' => t('Custom'),
316
    'none' => t('No gallery'),
317
  );
318
  $element['colorbox_gallery'] = array(
319
    '#title' => t('Gallery (image grouping)'),
320
    '#type' => 'select',
321
    '#default_value' => $settings['colorbox_gallery'],
322
    '#options' => $gallery,
323
    '#description' => t('How Colorbox should group the image galleries.'),
324
  );
325
  $element['colorbox_gallery_custom'] = array(
326
    '#title' => t('Custom gallery'),
327
    '#type' => 'textfield',
328
    '#maxlength' => 32,
329
    '#default_value' => $settings['colorbox_gallery_custom'],
330
    '#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.'),
331
    '#element_validate' => array('colorbox_gallery_custom_validate'),
332
    '#required' => FALSE,
333
    '#states' => array(
334
      'visible' => array(
335
        ':input[name$="[settings_edit_form][settings][colorbox_gallery]"]' => array('value' => 'custom'),
336
      ),
337
    ),
338
  );
339

    
340
  $caption = array(
341
    'auto' =>  t('Automatic'),
342
    'title' => t('Title text'),
343
    'alt' => t('Alt text'),
344
    'node_title' => t('Content title'),
345
    'custom' => t('Custom (with tokens)'),
346
    'none' => t('None'),
347
  );
348
  $element['colorbox_caption'] = array(
349
    '#title' => t('Caption'),
350
    '#type' => 'select',
351
    '#default_value' => $settings['colorbox_caption'],
352
    '#options' => $caption,
353
    '#description' => t('Automatic will use the first non-empty value of the title, the alt text and the content title.'),
354
  );
355
  $element['colorbox_caption_custom'] = array(
356
    '#title' => t('Custom caption'),
357
    '#type' => 'textfield',
358
    '#default_value' => $settings['colorbox_caption_custom'],
359
    '#states' => array(
360
      'visible' => array(
361
        ':input[name$="[settings_edit_form][settings][colorbox_caption]"]' => array('value' => 'custom'),
362
      ),
363
    ),
364
  );
365
  // Allow users to hide or set a custom recursion limit.
366
  // The module token_tweaks sets a global recursion limit that can not be bypassed.
367
  if (module_exists('token') && $recursion_limit = min(variable_get('token_tree_recursion_limit', 3), variable_get('colorbox_token_recursion_limit', 3))) {
368
    // File entities do not have $field, only $instance.
369
    if (!empty($field)) {
370
      $token_types = array_merge(array_keys($field['bundles']), array('file'));
371
    }
372
    else {
373
      $token_types = array($instance['entity_type'], 'file');
374
    }
375
    $element['colorbox_token'] = array(
376
      '#type' => 'fieldset',
377
      '#title' => t('Replacement patterns'),
378
      '#theme' => 'token_tree',
379
      '#token_types' => $token_types,
380
      '#recursion_limit' => $recursion_limit,
381
      '#dialog' => TRUE,
382
      '#states' => array(
383
        'visible' => array(
384
          ':input[name$="[settings_edit_form][settings][colorbox_caption]"]' => array('value' => 'custom'),
385
        ),
386
      ),
387
    );
388
  }
389
  else {
390
    $element['colorbox_token'] = array(
391
      '#type' => 'fieldset',
392
      '#title' => t('Replacement patterns'),
393
      '#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>',
394
      '#states' => array(
395
        'visible' => array(
396
          ':input[name$="[settings_edit_form][settings][colorbox_caption]"]' => array('value' => 'custom'),
397
        ),
398
      ),
399
    );
400
  }
401

    
402
  return $element;
403
}
404

    
405
/**
406
 * Validate function for colorbox_gallery_custom.
407
 */
408
function colorbox_gallery_custom_validate($element, &$form_state) {
409
  if (!empty($element['#value']) && !preg_match('!^[a-z0-9_-]+$!', $element['#value'])) {
410
    form_error($element, t('%name must only contain lowercase letters, numbers, hyphen and underscores.', array('%name' => $element['#title'])));
411
  }
412
}
413

    
414
/**
415
 * Implements hook_field_formatter_settings_summary().
416
 */
417
function colorbox_field_formatter_settings_summary($field, $instance, $view_mode) {
418
  $display = $instance['display'][$view_mode];
419
  $settings = $display['settings'];
420

    
421
  $summary = array();
422

    
423
  $image_styles = image_style_options(FALSE);
424
  // Unset possible 'No defined styles' option.
425
  unset($image_styles['']);
426
  // Styles could be lost because of enabled/disabled modules that defines
427
  // their styles in code.
428
  if (isset($image_styles[$settings['colorbox_node_style']])) {
429
    $summary[] = t('Content image style: @style', array('@style' => $image_styles[$settings['colorbox_node_style']]));
430
  }
431
  elseif ($settings['colorbox_node_style'] == 'hide') {
432
    $summary[] = t('Content image style: Hide');
433
  }
434
  else {
435
    $summary[] = t('Content image style: Original image');
436
  }
437

    
438
  if (isset($image_styles[$settings['colorbox_node_style_first']])) {
439
    $summary[] = t('Content image style of first image: @style', array('@style' => $image_styles[$settings['colorbox_node_style_first']]));
440
  }
441

    
442
  if (isset($image_styles[$settings['colorbox_image_style']])) {
443
    $summary[] = t('Colorbox image style: @style', array('@style' => $image_styles[$settings['colorbox_image_style']]));
444
  }
445
  else {
446
    $summary[] = t('Colorbox image style: Original image');
447
  }
448

    
449
  $gallery = array(
450
    'post' => t('Per post gallery'),
451
    'page' => t('Per page gallery'),
452
    'field_post' => t('Per field in post gallery'),
453
    'field_page' => t('Per field in page gallery'),
454
    'custom' => t('Custom'),
455
    'none' => t('No gallery'),
456
  );
457
  if (isset($settings['colorbox_gallery'])) {
458
    $summary[] = t('Colorbox gallery type: @type', array('@type' => $gallery[$settings['colorbox_gallery']])) . ($settings['colorbox_gallery'] == 'custom' ? ' (' . $settings['colorbox_gallery_custom'] . ')' : '');
459
  }
460

    
461
  $caption = array(
462
    'auto' =>  t('Automatic'),
463
    'title' => t('Title text'),
464
    'alt' => t('Alt text'),
465
    'node_title' => t('Content title'),
466
    'custom' => t('Custom (with tokens)'),
467
    'none' => t('None'),
468
  );
469
  if (isset($settings['colorbox_caption'])) {
470
    $summary[] = t('Colorbox caption: @type', array('@type' => $caption[$settings['colorbox_caption']]));
471
  }
472

    
473
  return implode('<br />', $summary);
474
}
475

    
476
/**
477
 * Implements hook_field_formatter_view().
478
 */
479
function colorbox_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
480
  $element = array();
481
  $index = $display['settings']['colorbox_multivalue_index'];
482

    
483
  foreach ($items as $delta => $item) {
484
    if ($index === NULL || $index === $delta) {
485
      $element[$delta] = array(
486
        '#theme' => 'colorbox_image_formatter',
487
        '#item' => $item,
488
        '#entity_type' => $entity_type,
489
        '#entity' => $entity,
490
        '#node' => $entity, // Left for legacy support.
491
        '#field' => $field,
492
        '#display_settings' => $display['settings'],
493
        '#delta' => $delta,
494
      );
495
    }
496
  }
497

    
498
  return $element;
499
}
500

    
501
/**
502
 * Implements hook_insert_styles().
503
 *
504
 * @return array
505
 */
506
function colorbox_insert_styles() {
507
  $insert_styles = array();
508
  foreach (image_styles() as $key => $style) {
509
    $label = isset($style['label']) ? $style['label'] : $style['name'];
510
    $insert_styles['colorbox__' . $key] = array('label' => t('Colorbox @style', array('@style' => $label)));
511
  }
512

    
513
  return $insert_styles;
514
}
515

    
516
/**
517
 * Implements hook_insert_content().
518
 */
519
function colorbox_insert_content($item, $style, $widget) {
520
  list($item['module_name'], $item['style_name']) = explode('__', $style['name'], 2);
521
  return theme(array('colorbox_insert_image__' . str_replace('-', '_', $item['style_name']), 'colorbox_insert_image'), array('item' => $item, 'widget' => $widget));
522
}
523

    
524
/**
525
 * Machine names normally need to be unique but that does not apply to galleries.
526
 *
527
 * @return false
528
 *   Always FALSE
529
 */
530
function colorbox_gallery_exists() {
531
  return FALSE;
532
}