colorbox.module 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. <?php
  2. /**
  3. * @file
  4. * A light-weight, customizable lightbox plugin for jQuery 1.3
  5. */
  6. /**
  7. * The minimum required version of the Colorbox plugin.
  8. */
  9. define('COLORBOX_MIN_PLUGIN_VERSION', '1.6.1');
  10. /**
  11. * Implements hook_theme().
  12. */
  13. function colorbox_theme() {
  14. return array(
  15. 'colorbox_imagefield' => array(
  16. 'variables' => array(
  17. 'image' => array(),
  18. 'path' => NULL,
  19. 'title' => NULL,
  20. 'gid' => NULL,
  21. ),
  22. 'file' => 'colorbox.theme.inc',
  23. ),
  24. 'colorbox_insert_image' => array(
  25. 'variables' => array(
  26. 'item' => NULL,
  27. 'widget' => NULL,
  28. ),
  29. 'template' => 'colorbox-insert-image',
  30. 'pattern' => 'colorbox_insert_image__[a-z0-9_]+',
  31. 'file' => 'colorbox.theme.inc',
  32. ),
  33. 'colorbox_image_formatter' => array(
  34. 'variables' => array(
  35. 'item' => NULL,
  36. 'entity_type' => NULL,
  37. 'entity' => NULL,
  38. 'node' => NULL, // Left for legacy support.
  39. 'field' => array(),
  40. 'display_settings' => array(),
  41. 'delta' => null,
  42. ),
  43. 'file' => 'colorbox.theme.inc',
  44. ),
  45. );
  46. }
  47. /**
  48. * Implements hook_init().
  49. */
  50. function colorbox_init() {
  51. // Do not load colorbox during the Drupal installation process, e.g. if part
  52. // of installation profiles. Only add the JavaScript and CSS on specified paths.
  53. if (!drupal_installation_attempted() && _colorbox_active()) {
  54. _colorbox_doheader();
  55. }
  56. }
  57. /**
  58. * Implements hook_views_api().
  59. */
  60. function colorbox_views_api() {
  61. return array(
  62. 'api' => 2,
  63. 'path' => drupal_get_path('module', 'colorbox') . '/views',
  64. );
  65. }
  66. /**
  67. * Implements hook_libraries_info().
  68. */
  69. function colorbox_libraries_info() {
  70. $libraries['colorbox'] = array(
  71. 'name' => 'Colorbox plugin',
  72. 'vendor url' => 'http://www.jacklmoore.com/colorbox',
  73. 'download url' => 'https://github.com/jackmoore/colorbox/archive/1.x.zip',
  74. 'version arguments' => array(
  75. 'file' => 'jquery.colorbox-min.js',
  76. 'pattern' => '@(?i:Colorbox)\sv?([0-9\.a-z]+)@',
  77. 'lines' => 5,
  78. ),
  79. 'files' => array(
  80. 'js' => array(
  81. 'jquery.colorbox-min.js',
  82. ),
  83. ),
  84. 'variants' => array(
  85. 'minified' => array(
  86. 'files' => array(
  87. 'js' => array(
  88. 'jquery.colorbox-min.js',
  89. ),
  90. ),
  91. ),
  92. 'source' => array(
  93. 'files' => array(
  94. 'js' => array(
  95. 'jquery.colorbox.js',
  96. ),
  97. ),
  98. ),
  99. ),
  100. );
  101. return $libraries;
  102. }
  103. /**
  104. * Implements hook_menu().
  105. */
  106. function colorbox_menu() {
  107. $items = array();
  108. $items['admin/config/media/colorbox'] = array(
  109. 'title' => 'Colorbox',
  110. 'description' => 'Adjust Colorbox settings.',
  111. 'file' => 'colorbox.admin.inc',
  112. 'page callback' => 'drupal_get_form',
  113. 'page arguments' => array('colorbox_admin_settings'),
  114. 'access arguments' => array('administer site configuration'),
  115. );
  116. return $items;
  117. }
  118. /**
  119. * Check if Colorbox should be active for the current URL.
  120. *
  121. * @return bool
  122. * TRUE if Colorbox should be active for the current page.
  123. */
  124. function _colorbox_active() {
  125. // Make it possible deactivate Colorbox with
  126. // parameter ?colorbox=no in the url.
  127. if (isset($_GET['colorbox']) && $_GET['colorbox'] == 'no') {
  128. return FALSE;
  129. }
  130. // Code from the block_list function in block.module.
  131. $path = drupal_get_path_alias($_GET['q']);
  132. $colorbox_pages = variable_get('colorbox_pages', "admin*\nimagebrowser*\nimg_assist*\nimce*\nnode/add/*\nnode/*/edit\nprint/*\nprintpdf/*\nsystem/ajax\nsystem/ajax/*");
  133. // Compare with the internal and path alias (if any).
  134. $page_match = drupal_match_path($path, $colorbox_pages);
  135. if ($path != $_GET['q']) {
  136. $page_match = $page_match || drupal_match_path($_GET['q'], $colorbox_pages);
  137. }
  138. $page_match = variable_get('colorbox_visibility', 0) == 0 ? !$page_match : $page_match;
  139. // Allow other modules to change the state of colorbox for the current URL.
  140. drupal_alter('colorbox_active', $page_match);
  141. return $page_match;
  142. }
  143. /**
  144. * Loads the various js and css files.
  145. */
  146. function _colorbox_doheader() {
  147. static $already_added = FALSE;
  148. if ($already_added) {
  149. return; // Don't add the JavaScript and CSS multiple times.
  150. }
  151. // Insert options and translated strings as javascript settings.
  152. if (variable_get('colorbox_custom_settings_activate', 0)) {
  153. $js_settings = array(
  154. 'transition' => variable_get('colorbox_transition_type', 'elastic'),
  155. 'speed' => variable_get('colorbox_transition_speed', 350),
  156. 'opacity' => variable_get('colorbox_opacity', '0.85'),
  157. 'slideshow' => variable_get('colorbox_slideshow', 0) ? TRUE : FALSE,
  158. 'slideshowAuto' => variable_get('colorbox_slideshowauto', 1) ? TRUE : FALSE,
  159. 'slideshowSpeed' => variable_get('colorbox_slideshowspeed', 2500),
  160. 'slideshowStart' => variable_get('colorbox_text_start', 'start slideshow'),
  161. 'slideshowStop' => variable_get('colorbox_text_stop', 'stop slideshow'),
  162. 'current' => strip_tags(variable_get('colorbox_text_current', '{current} of {total}')),
  163. 'previous' => strip_tags(variable_get('colorbox_text_previous', '« Prev')),
  164. 'next' => strip_tags(variable_get('colorbox_text_next', 'Next »')),
  165. 'close' => strip_tags(variable_get('colorbox_text_close', 'Close')),
  166. 'overlayClose' => variable_get('colorbox_overlayclose', 1) ? TRUE : FALSE,
  167. 'maxWidth' => variable_get('colorbox_maxwidth', '98%'),
  168. 'maxHeight' => variable_get('colorbox_maxheight', '98%'),
  169. 'initialWidth' => variable_get('colorbox_initialwidth', '300'),
  170. 'initialHeight' => variable_get('colorbox_initialheight', '250'),
  171. 'fixed' => variable_get('colorbox_fixed', 1) ? TRUE : FALSE,
  172. 'scrolling' => variable_get('colorbox_scrolling', 1) ? TRUE : FALSE,
  173. 'mobiledetect' => variable_get('colorbox_mobile_detect', 1) ? TRUE : FALSE,
  174. 'mobiledevicewidth' => variable_get('colorbox_mobile_device_width', '480px'),
  175. );
  176. }
  177. else {
  178. $js_settings = array(
  179. 'opacity' => '0.85',
  180. 'current' => t('{current} of {total}'),
  181. 'previous' => t('« Prev'),
  182. 'next' => t('Next »'),
  183. 'close' => t('Close'),
  184. 'maxWidth' => '98%',
  185. 'maxHeight' => '98%',
  186. 'fixed' => TRUE,
  187. 'mobiledetect' => variable_get('colorbox_mobile_detect', 1) ? TRUE : FALSE,
  188. 'mobiledevicewidth' => variable_get('colorbox_mobile_device_width', '480px'),
  189. );
  190. }
  191. $path = drupal_get_path('module', 'colorbox');
  192. $style = variable_get('colorbox_style', 'default');
  193. // Give other modules the possibility to override Colorbox settings and style.
  194. $data = &$js_settings;
  195. drupal_alter('colorbox_settings', $data, $style);
  196. drupal_add_js(array('colorbox' => $js_settings), array('type' => 'setting', 'scope' => JS_DEFAULT));
  197. // Add and initialise the Colorbox plugin.
  198. $variant = variable_get('colorbox_compression_type', 'minified');
  199. if (module_exists('libraries')) {
  200. libraries_load('colorbox', $variant);
  201. }
  202. drupal_add_js($path . '/js/colorbox.js');
  203. // Add JS and CSS based on selected style.
  204. switch ($style) {
  205. case 'none':
  206. break;
  207. case 'default':
  208. case 'plain':
  209. case 'stockholmsyndrome':
  210. drupal_add_css($path . '/styles/' . $style . '/colorbox_style.css');
  211. drupal_add_js($path . '/styles/' . $style . '/colorbox_style.js');
  212. break;
  213. default:
  214. drupal_add_css($style . '/colorbox.css');
  215. }
  216. if (variable_get('colorbox_load', 0)) {
  217. drupal_add_js($path . '/js/colorbox_load.js');
  218. }
  219. if (variable_get('colorbox_inline', 0)) {
  220. drupal_add_js($path . '/js/colorbox_inline.js');
  221. }
  222. $already_added = TRUE;
  223. }
  224. /**
  225. * Implements hook_field_formatter_info().
  226. */
  227. function colorbox_field_formatter_info() {
  228. return array(
  229. 'colorbox' => array(
  230. 'label' => t('Colorbox'),
  231. 'field types' => array('image'),
  232. 'settings' => array(
  233. 'colorbox_node_style' => '',
  234. 'colorbox_node_style_first' => '',
  235. 'colorbox_image_style' => '',
  236. 'colorbox_gallery' => 'post',
  237. 'colorbox_gallery_custom' => '',
  238. 'colorbox_caption' => 'auto',
  239. 'colorbox_caption_custom' => '',
  240. 'colorbox_multivalue_index' => NULL,
  241. ),
  242. ),
  243. );
  244. }
  245. /**
  246. * Implements hook_field_formatter_settings_form().
  247. */
  248. function colorbox_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  249. $display = $instance['display'][$view_mode];
  250. $settings = $display['settings'];
  251. $image_styles = image_style_options(FALSE);
  252. $image_styles_hide = $image_styles;
  253. $image_styles_hide['hide'] = t('Hide (do not display image)');
  254. $element['colorbox_node_style'] = array(
  255. '#title' => t('Content image style'),
  256. '#type' => 'select',
  257. '#default_value' => $settings['colorbox_node_style'],
  258. '#empty_option' => t('None (original image)'),
  259. '#options' => $image_styles_hide,
  260. '#description' => t('Image style to use in the content.'),
  261. );
  262. $element['colorbox_node_style_first'] = array(
  263. '#title' => t('Content image style for first image'),
  264. '#type' => 'select',
  265. '#default_value' => $settings['colorbox_node_style_first'],
  266. '#empty_option' => t('No special style.'),
  267. '#options' => $image_styles,
  268. '#description' => t('Image style to use in the content for the first image.'),
  269. );
  270. $element['colorbox_image_style'] = array(
  271. '#title' => t('Colorbox image style'),
  272. '#type' => 'select',
  273. '#default_value' => $settings['colorbox_image_style'],
  274. '#empty_option' => t('None (original image)'),
  275. '#options' => $image_styles,
  276. '#description' => t('Image style to use in the Colorbox.'),
  277. );
  278. $gallery = array(
  279. 'post' => t('Per post gallery'),
  280. 'page' => t('Per page gallery'),
  281. 'field_post' => t('Per field in post gallery'),
  282. 'field_page' => t('Per field in page gallery'),
  283. 'custom' => t('Custom'),
  284. 'none' => t('No gallery'),
  285. );
  286. $element['colorbox_gallery'] = array(
  287. '#title' => t('Gallery (image grouping)'),
  288. '#type' => 'select',
  289. '#default_value' => $settings['colorbox_gallery'],
  290. '#options' => $gallery,
  291. '#description' => t('How Colorbox should group the image galleries.'),
  292. );
  293. $element['colorbox_gallery_custom'] = array(
  294. '#title' => t('Custom gallery'),
  295. '#type' => 'textfield',
  296. '#maxlength' => 32,
  297. '#default_value' => $settings['colorbox_gallery_custom'],
  298. '#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.'),
  299. '#element_validate' => array('colorbox_gallery_custom_validate'),
  300. '#required' => FALSE,
  301. '#states' => array(
  302. 'visible' => array(
  303. ':input[name$="[settings_edit_form][settings][colorbox_gallery]"]' => array('value' => 'custom'),
  304. ),
  305. ),
  306. );
  307. $caption = array(
  308. 'auto' => t('Automatic'),
  309. 'title' => t('Title text'),
  310. 'alt' => t('Alt text'),
  311. 'node_title' => t('Content title'),
  312. 'custom' => t('Custom (with tokens)'),
  313. 'none' => t('None'),
  314. );
  315. $element['colorbox_caption'] = array(
  316. '#title' => t('Caption'),
  317. '#type' => 'select',
  318. '#default_value' => $settings['colorbox_caption'],
  319. '#options' => $caption,
  320. '#description' => t('Automatic will use the first non-empty value of the title, the alt text and the content title.'),
  321. );
  322. $element['colorbox_caption_custom'] = array(
  323. '#title' => t('Custom caption'),
  324. '#type' => 'textfield',
  325. '#default_value' => $settings['colorbox_caption_custom'],
  326. '#states' => array(
  327. 'visible' => array(
  328. ':input[name$="[settings_edit_form][settings][colorbox_caption]"]' => array('value' => 'custom'),
  329. ),
  330. ),
  331. );
  332. // Allow users to hide or set a custom recursion limit.
  333. // The module token_tweaks sets a global recursion limit that can not be bypassed.
  334. if (module_exists('token') && $recursion_limit = min(variable_get('token_tree_recursion_limit', 3), variable_get('colorbox_token_recursion_limit', 3))) {
  335. $element['colorbox_token'] = array(
  336. '#type' => 'fieldset',
  337. '#title' => t('Replacement patterns'),
  338. '#theme' => 'token_tree',
  339. '#token_types' => array_merge(array_keys($field['bundles']),array('file')),
  340. '#recursion_limit' => $recursion_limit,
  341. '#dialog' => TRUE,
  342. '#states' => array(
  343. 'visible' => array(
  344. ':input[name$="[settings_edit_form][settings][colorbox_caption]"]' => array('value' => 'custom'),
  345. ),
  346. ),
  347. );
  348. }
  349. else {
  350. $element['colorbox_token'] = array(
  351. '#type' => 'fieldset',
  352. '#title' => t('Replacement patterns'),
  353. '#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>',
  354. '#states' => array(
  355. 'visible' => array(
  356. ':input[name$="[settings_edit_form][settings][colorbox_caption]"]' => array('value' => 'custom'),
  357. ),
  358. ),
  359. );
  360. }
  361. return $element;
  362. }
  363. /**
  364. * Validate function for colorbox_gallery_custom.
  365. */
  366. function colorbox_gallery_custom_validate($element, &$form_state) {
  367. if (!empty($element['#value']) && !preg_match('!^[a-z0-9_-]+$!', $element['#value'])) {
  368. form_error($element, t('%name must only contain lowercase letters, numbers, hyphen and underscores.', array('%name' => $element['#title'])));
  369. }
  370. }
  371. /**
  372. * Implements hook_field_formatter_settings_summary().
  373. */
  374. function colorbox_field_formatter_settings_summary($field, $instance, $view_mode) {
  375. $display = $instance['display'][$view_mode];
  376. $settings = $display['settings'];
  377. $summary = array();
  378. $image_styles = image_style_options(FALSE);
  379. // Unset possible 'No defined styles' option.
  380. unset($image_styles['']);
  381. // Styles could be lost because of enabled/disabled modules that defines
  382. // their styles in code.
  383. if (isset($image_styles[$settings['colorbox_node_style']])) {
  384. $summary[] = t('Content image style: @style', array('@style' => $image_styles[$settings['colorbox_node_style']]));
  385. }
  386. elseif ($settings['colorbox_node_style'] == 'hide') {
  387. $summary[] = t('Content image style: Hide');
  388. }
  389. else {
  390. $summary[] = t('Content image style: Original image');
  391. }
  392. if (isset($image_styles[$settings['colorbox_node_style_first']])) {
  393. $summary[] = t('Content image style of first image: @style', array('@style' => $image_styles[$settings['colorbox_node_style_first']]));
  394. }
  395. if (isset($image_styles[$settings['colorbox_image_style']])) {
  396. $summary[] = t('Colorbox image style: @style', array('@style' => $image_styles[$settings['colorbox_image_style']]));
  397. }
  398. else {
  399. $summary[] = t('Colorbox image style: Original image');
  400. }
  401. $gallery = array(
  402. 'post' => t('Per post gallery'),
  403. 'page' => t('Per page gallery'),
  404. 'field_post' => t('Per field in post gallery'),
  405. 'field_page' => t('Per field in page gallery'),
  406. 'custom' => t('Custom'),
  407. 'none' => t('No gallery'),
  408. );
  409. if (isset($settings['colorbox_gallery'])) {
  410. $summary[] = t('Colorbox gallery type: @type', array('@type' => $gallery[$settings['colorbox_gallery']])) . ($settings['colorbox_gallery'] == 'custom' ? ' (' . $settings['colorbox_gallery_custom'] . ')' : '');
  411. }
  412. $caption = array(
  413. 'auto' => t('Automatic'),
  414. 'title' => t('Title text'),
  415. 'alt' => t('Alt text'),
  416. 'node_title' => t('Content title'),
  417. 'custom' => t('Custom (with tokens)'),
  418. 'none' => t('None'),
  419. );
  420. if (isset($settings['colorbox_caption'])) {
  421. $summary[] = t('Colorbox caption: @type', array('@type' => $caption[$settings['colorbox_caption']]));
  422. }
  423. return implode('<br />', $summary);
  424. }
  425. /**
  426. * Implements hook_field_formatter_view().
  427. */
  428. function colorbox_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  429. $element = array();
  430. $index = $display['settings']['colorbox_multivalue_index'];
  431. foreach ($items as $delta => $item) {
  432. if ($index === NULL || $index === $delta) {
  433. $element[$delta] = array(
  434. '#theme' => 'colorbox_image_formatter',
  435. '#item' => $item,
  436. '#entity_type' => $entity_type,
  437. '#entity' => $entity,
  438. '#node' => $entity, // Left for legacy support.
  439. '#field' => $field,
  440. '#display_settings' => $display['settings'],
  441. '#delta' => $delta,
  442. );
  443. }
  444. }
  445. return $element;
  446. }
  447. /**
  448. * Implements hook_insert_styles().
  449. *
  450. * @return array
  451. */
  452. function colorbox_insert_styles() {
  453. $insert_styles = array();
  454. foreach (image_styles() as $key => $style) {
  455. $label = isset($style['label']) ? $style['label'] : $style['name'];
  456. $insert_styles['colorbox__' . $key] = array('label' => t('Colorbox @style', array('@style' => $label)));
  457. }
  458. return $insert_styles;
  459. }
  460. /**
  461. * Implements hook_insert_content().
  462. */
  463. function colorbox_insert_content($item, $style, $widget) {
  464. list($item['module_name'], $item['style_name']) = explode('__', $style['name'], 2);
  465. return theme(array('colorbox_insert_image__' . str_replace('-', '_', $item['style_name']), 'colorbox_insert_image'), array('item' => $item, 'widget' => $widget));
  466. }
  467. /**
  468. * Machine names normally need to be unique but that does not apply to galleries.
  469. *
  470. * @return false
  471. * Always FALSE
  472. */
  473. function colorbox_gallery_exists() {
  474. return FALSE;
  475. }