colorbox.module 17 KB

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