colorbox.module 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. 'returnFocus' => variable_get('colorbox_returnfocus', 1) ? TRUE : FALSE,
  168. 'maxWidth' => variable_get('colorbox_maxwidth', '98%'),
  169. 'maxHeight' => variable_get('colorbox_maxheight', '98%'),
  170. 'initialWidth' => variable_get('colorbox_initialwidth', '300'),
  171. 'initialHeight' => variable_get('colorbox_initialheight', '250'),
  172. 'fixed' => variable_get('colorbox_fixed', 1) ? TRUE : FALSE,
  173. 'scrolling' => variable_get('colorbox_scrolling', 1) ? TRUE : FALSE,
  174. 'mobiledetect' => variable_get('colorbox_mobile_detect', 1) ? TRUE : FALSE,
  175. 'mobiledevicewidth' => variable_get('colorbox_mobile_device_width', '480px'),
  176. );
  177. }
  178. else {
  179. $js_settings = array(
  180. 'opacity' => '0.85',
  181. 'current' => t('{current} of {total}'),
  182. 'previous' => t('« Prev'),
  183. 'next' => t('Next »'),
  184. 'close' => t('Close'),
  185. 'maxWidth' => '98%',
  186. 'maxHeight' => '98%',
  187. 'fixed' => TRUE,
  188. 'mobiledetect' => variable_get('colorbox_mobile_detect', 1) ? TRUE : FALSE,
  189. 'mobiledevicewidth' => variable_get('colorbox_mobile_device_width', '480px'),
  190. );
  191. }
  192. $path = drupal_get_path('module', 'colorbox');
  193. $style = variable_get('colorbox_style', 'default');
  194. // Give other modules the possibility to override Colorbox settings and style.
  195. $data = &$js_settings;
  196. drupal_alter('colorbox_settings', $data, $style);
  197. drupal_add_js(array('colorbox' => $js_settings), array('type' => 'setting', 'scope' => JS_DEFAULT));
  198. // Add and initialise the Colorbox plugin.
  199. $variant = variable_get('colorbox_compression_type', 'minified');
  200. if (module_exists('libraries')) {
  201. libraries_load('colorbox', $variant);
  202. }
  203. drupal_add_js($path . '/js/colorbox.js');
  204. // Add JS and CSS based on selected style.
  205. switch ($style) {
  206. case 'none':
  207. break;
  208. case 'default':
  209. case 'plain':
  210. case 'stockholmsyndrome':
  211. drupal_add_css($path . '/styles/' . $style . '/colorbox_style.css');
  212. drupal_add_js($path . '/styles/' . $style . '/colorbox_style.js');
  213. break;
  214. default:
  215. drupal_add_css($style . '/colorbox.css');
  216. }
  217. if (variable_get('colorbox_load', 0)) {
  218. drupal_add_js($path . '/js/colorbox_load.js');
  219. }
  220. if (variable_get('colorbox_inline', 0)) {
  221. drupal_add_js($path . '/js/colorbox_inline.js');
  222. }
  223. $already_added = TRUE;
  224. }
  225. /**
  226. * Implements hook_field_formatter_info().
  227. */
  228. function colorbox_field_formatter_info() {
  229. return array(
  230. 'colorbox' => array(
  231. 'label' => t('Colorbox'),
  232. 'field types' => array('image'),
  233. 'settings' => array(
  234. 'colorbox_node_style' => '',
  235. 'colorbox_node_style_first' => '',
  236. 'colorbox_image_style' => '',
  237. 'colorbox_gallery' => 'post',
  238. 'colorbox_gallery_custom' => '',
  239. 'colorbox_caption' => 'auto',
  240. 'colorbox_caption_custom' => '',
  241. 'colorbox_multivalue_index' => NULL,
  242. ),
  243. ),
  244. );
  245. }
  246. /**
  247. * Implements hook_field_formatter_settings_form().
  248. */
  249. function colorbox_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  250. $display = $instance['display'][$view_mode];
  251. $settings = $display['settings'];
  252. $image_styles = image_style_options(FALSE);
  253. $image_styles_hide = $image_styles;
  254. $image_styles_hide['hide'] = t('Hide (do not display image)');
  255. $element['colorbox_node_style'] = array(
  256. '#title' => t('Content image style'),
  257. '#type' => 'select',
  258. '#default_value' => $settings['colorbox_node_style'],
  259. '#empty_option' => t('None (original image)'),
  260. '#options' => $image_styles_hide,
  261. '#description' => t('Image style to use in the content.'),
  262. );
  263. $element['colorbox_node_style_first'] = array(
  264. '#title' => t('Content image style for first image'),
  265. '#type' => 'select',
  266. '#default_value' => $settings['colorbox_node_style_first'],
  267. '#empty_option' => t('No special style.'),
  268. '#options' => $image_styles,
  269. '#description' => t('Image style to use in the content for the first image.'),
  270. );
  271. $element['colorbox_image_style'] = array(
  272. '#title' => t('Colorbox image style'),
  273. '#type' => 'select',
  274. '#default_value' => $settings['colorbox_image_style'],
  275. '#empty_option' => t('None (original image)'),
  276. '#options' => $image_styles,
  277. '#description' => t('Image style to use in the Colorbox.'),
  278. );
  279. $gallery = array(
  280. 'post' => t('Per post gallery'),
  281. 'page' => t('Per page gallery'),
  282. 'field_post' => t('Per field in post gallery'),
  283. 'field_page' => t('Per field in page gallery'),
  284. 'custom' => t('Custom'),
  285. 'none' => t('No gallery'),
  286. );
  287. $element['colorbox_gallery'] = array(
  288. '#title' => t('Gallery (image grouping)'),
  289. '#type' => 'select',
  290. '#default_value' => $settings['colorbox_gallery'],
  291. '#options' => $gallery,
  292. '#description' => t('How Colorbox should group the image galleries.'),
  293. );
  294. $element['colorbox_gallery_custom'] = array(
  295. '#title' => t('Custom gallery'),
  296. '#type' => 'textfield',
  297. '#maxlength' => 32,
  298. '#default_value' => $settings['colorbox_gallery_custom'],
  299. '#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.'),
  300. '#element_validate' => array('colorbox_gallery_custom_validate'),
  301. '#required' => FALSE,
  302. '#states' => array(
  303. 'visible' => array(
  304. ':input[name$="[settings_edit_form][settings][colorbox_gallery]"]' => array('value' => 'custom'),
  305. ),
  306. ),
  307. );
  308. $caption = array(
  309. 'auto' => t('Automatic'),
  310. 'title' => t('Title text'),
  311. 'alt' => t('Alt text'),
  312. 'node_title' => t('Content title'),
  313. 'custom' => t('Custom (with tokens)'),
  314. 'none' => t('None'),
  315. );
  316. $element['colorbox_caption'] = array(
  317. '#title' => t('Caption'),
  318. '#type' => 'select',
  319. '#default_value' => $settings['colorbox_caption'],
  320. '#options' => $caption,
  321. '#description' => t('Automatic will use the first non-empty value of the title, the alt text and the content title.'),
  322. );
  323. $element['colorbox_caption_custom'] = array(
  324. '#title' => t('Custom caption'),
  325. '#type' => 'textfield',
  326. '#default_value' => $settings['colorbox_caption_custom'],
  327. '#states' => array(
  328. 'visible' => array(
  329. ':input[name$="[settings_edit_form][settings][colorbox_caption]"]' => array('value' => 'custom'),
  330. ),
  331. ),
  332. );
  333. // Allow users to hide or set a custom recursion limit.
  334. // The module token_tweaks sets a global recursion limit that can not be bypassed.
  335. if (module_exists('token') && $recursion_limit = min(variable_get('token_tree_recursion_limit', 3), variable_get('colorbox_token_recursion_limit', 3))) {
  336. // File entities do not have $field, only $instance.
  337. if (!empty($field)) {
  338. $token_types = array_merge(array_keys($field['bundles']), array('file'));
  339. }
  340. else {
  341. $token_types = array($instance['entity_type'], 'file');
  342. }
  343. $element['colorbox_token'] = array(
  344. '#type' => 'fieldset',
  345. '#title' => t('Replacement patterns'),
  346. '#theme' => 'token_tree',
  347. '#token_types' => $token_types,
  348. '#recursion_limit' => $recursion_limit,
  349. '#dialog' => TRUE,
  350. '#states' => array(
  351. 'visible' => array(
  352. ':input[name$="[settings_edit_form][settings][colorbox_caption]"]' => array('value' => 'custom'),
  353. ),
  354. ),
  355. );
  356. }
  357. else {
  358. $element['colorbox_token'] = array(
  359. '#type' => 'fieldset',
  360. '#title' => t('Replacement patterns'),
  361. '#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>',
  362. '#states' => array(
  363. 'visible' => array(
  364. ':input[name$="[settings_edit_form][settings][colorbox_caption]"]' => array('value' => 'custom'),
  365. ),
  366. ),
  367. );
  368. }
  369. return $element;
  370. }
  371. /**
  372. * Validate function for colorbox_gallery_custom.
  373. */
  374. function colorbox_gallery_custom_validate($element, &$form_state) {
  375. if (!empty($element['#value']) && !preg_match('!^[a-z0-9_-]+$!', $element['#value'])) {
  376. form_error($element, t('%name must only contain lowercase letters, numbers, hyphen and underscores.', array('%name' => $element['#title'])));
  377. }
  378. }
  379. /**
  380. * Implements hook_field_formatter_settings_summary().
  381. */
  382. function colorbox_field_formatter_settings_summary($field, $instance, $view_mode) {
  383. $display = $instance['display'][$view_mode];
  384. $settings = $display['settings'];
  385. $summary = array();
  386. $image_styles = image_style_options(FALSE);
  387. // Unset possible 'No defined styles' option.
  388. unset($image_styles['']);
  389. // Styles could be lost because of enabled/disabled modules that defines
  390. // their styles in code.
  391. if (isset($image_styles[$settings['colorbox_node_style']])) {
  392. $summary[] = t('Content image style: @style', array('@style' => $image_styles[$settings['colorbox_node_style']]));
  393. }
  394. elseif ($settings['colorbox_node_style'] == 'hide') {
  395. $summary[] = t('Content image style: Hide');
  396. }
  397. else {
  398. $summary[] = t('Content image style: Original image');
  399. }
  400. if (isset($image_styles[$settings['colorbox_node_style_first']])) {
  401. $summary[] = t('Content image style of first image: @style', array('@style' => $image_styles[$settings['colorbox_node_style_first']]));
  402. }
  403. if (isset($image_styles[$settings['colorbox_image_style']])) {
  404. $summary[] = t('Colorbox image style: @style', array('@style' => $image_styles[$settings['colorbox_image_style']]));
  405. }
  406. else {
  407. $summary[] = t('Colorbox image style: Original image');
  408. }
  409. $gallery = array(
  410. 'post' => t('Per post gallery'),
  411. 'page' => t('Per page gallery'),
  412. 'field_post' => t('Per field in post gallery'),
  413. 'field_page' => t('Per field in page gallery'),
  414. 'custom' => t('Custom'),
  415. 'none' => t('No gallery'),
  416. );
  417. if (isset($settings['colorbox_gallery'])) {
  418. $summary[] = t('Colorbox gallery type: @type', array('@type' => $gallery[$settings['colorbox_gallery']])) . ($settings['colorbox_gallery'] == 'custom' ? ' (' . $settings['colorbox_gallery_custom'] . ')' : '');
  419. }
  420. $caption = array(
  421. 'auto' => t('Automatic'),
  422. 'title' => t('Title text'),
  423. 'alt' => t('Alt text'),
  424. 'node_title' => t('Content title'),
  425. 'custom' => t('Custom (with tokens)'),
  426. 'none' => t('None'),
  427. );
  428. if (isset($settings['colorbox_caption'])) {
  429. $summary[] = t('Colorbox caption: @type', array('@type' => $caption[$settings['colorbox_caption']]));
  430. }
  431. return implode('<br />', $summary);
  432. }
  433. /**
  434. * Implements hook_field_formatter_view().
  435. */
  436. function colorbox_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  437. $element = array();
  438. $index = $display['settings']['colorbox_multivalue_index'];
  439. foreach ($items as $delta => $item) {
  440. if ($index === NULL || $index === $delta) {
  441. $element[$delta] = array(
  442. '#theme' => 'colorbox_image_formatter',
  443. '#item' => $item,
  444. '#entity_type' => $entity_type,
  445. '#entity' => $entity,
  446. '#node' => $entity, // Left for legacy support.
  447. '#field' => $field,
  448. '#display_settings' => $display['settings'],
  449. '#delta' => $delta,
  450. );
  451. }
  452. }
  453. return $element;
  454. }
  455. /**
  456. * Implements hook_insert_styles().
  457. *
  458. * @return array
  459. */
  460. function colorbox_insert_styles() {
  461. $insert_styles = array();
  462. foreach (image_styles() as $key => $style) {
  463. $label = isset($style['label']) ? $style['label'] : $style['name'];
  464. $insert_styles['colorbox__' . $key] = array('label' => t('Colorbox @style', array('@style' => $label)));
  465. }
  466. return $insert_styles;
  467. }
  468. /**
  469. * Implements hook_insert_content().
  470. */
  471. function colorbox_insert_content($item, $style, $widget) {
  472. list($item['module_name'], $item['style_name']) = explode('__', $style['name'], 2);
  473. return theme(array('colorbox_insert_image__' . str_replace('-', '_', $item['style_name']), 'colorbox_insert_image'), array('item' => $item, 'widget' => $widget));
  474. }
  475. /**
  476. * Machine names normally need to be unique but that does not apply to galleries.
  477. *
  478. * @return false
  479. * Always FALSE
  480. */
  481. function colorbox_gallery_exists() {
  482. return FALSE;
  483. }