colorbox.module 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. return $page_match;
  140. }
  141. /**
  142. * Loads the various js and css files.
  143. */
  144. function _colorbox_doheader() {
  145. static $already_added = FALSE;
  146. if ($already_added) {
  147. return; // Don't add the JavaScript and CSS multiple times.
  148. }
  149. if (!_colorbox_active()) {
  150. return; // Don't add the JavaScript and CSS on specified paths.
  151. }
  152. // Insert options and translated strings as javascript settings.
  153. if (variable_get('colorbox_custom_settings_activate', 0)) {
  154. $js_settings = array(
  155. 'transition' => variable_get('colorbox_transition_type', 'elastic'),
  156. 'speed' => variable_get('colorbox_transition_speed', 350),
  157. 'opacity' => variable_get('colorbox_opacity', '0.85'),
  158. 'slideshow' => variable_get('colorbox_slideshow', 0) ? TRUE : FALSE,
  159. 'slideshowAuto' => variable_get('colorbox_slideshowauto', 1) ? TRUE : FALSE,
  160. 'slideshowSpeed' => variable_get('colorbox_slideshowspeed', 2500),
  161. 'slideshowStart' => variable_get('colorbox_text_start', 'start slideshow'),
  162. 'slideshowStop' => variable_get('colorbox_text_stop', 'stop slideshow'),
  163. 'current' => strip_tags(variable_get('colorbox_text_current', '{current} of {total}')),
  164. 'previous' => strip_tags(variable_get('colorbox_text_previous', '« Prev')),
  165. 'next' => strip_tags(variable_get('colorbox_text_next', 'Next »')),
  166. 'close' => strip_tags(variable_get('colorbox_text_close', 'Close')),
  167. 'overlayClose' => variable_get('colorbox_overlayclose', 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. $element['colorbox_token'] = array(
  337. '#type' => 'fieldset',
  338. '#title' => t('Replacement patterns'),
  339. '#theme' => 'token_tree',
  340. '#token_types' => array($instance['entity_type'], 'file'),
  341. '#recursion_limit' => $recursion_limit,
  342. '#dialog' => TRUE,
  343. '#states' => array(
  344. 'visible' => array(
  345. ':input[name$="[settings_edit_form][settings][colorbox_caption]"]' => array('value' => 'custom'),
  346. ),
  347. ),
  348. );
  349. }
  350. else {
  351. $element['colorbox_token'] = array(
  352. '#type' => 'fieldset',
  353. '#title' => t('Replacement patterns'),
  354. '#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>',
  355. '#states' => array(
  356. 'visible' => array(
  357. ':input[name$="[settings_edit_form][settings][colorbox_caption]"]' => array('value' => 'custom'),
  358. ),
  359. ),
  360. );
  361. }
  362. return $element;
  363. }
  364. /**
  365. * Validate function for colorbox_gallery_custom.
  366. */
  367. function colorbox_gallery_custom_validate($element, &$form_state) {
  368. if (!empty($element['#value']) && !preg_match('!^[a-z0-9_-]+$!', $element['#value'])) {
  369. form_error($element, t('%name must only contain lowercase letters, numbers, hyphen and underscores.', array('%name' => $element['#title'])));
  370. }
  371. }
  372. /**
  373. * Implements hook_field_formatter_settings_summary().
  374. */
  375. function colorbox_field_formatter_settings_summary($field, $instance, $view_mode) {
  376. $display = $instance['display'][$view_mode];
  377. $settings = $display['settings'];
  378. $summary = array();
  379. $image_styles = image_style_options(FALSE);
  380. // Unset possible 'No defined styles' option.
  381. unset($image_styles['']);
  382. // Styles could be lost because of enabled/disabled modules that defines
  383. // their styles in code.
  384. if (isset($image_styles[$settings['colorbox_node_style']])) {
  385. $summary[] = t('Content image style: @style', array('@style' => $image_styles[$settings['colorbox_node_style']]));
  386. }
  387. elseif ($settings['colorbox_node_style'] == 'hide') {
  388. $summary[] = t('Content image style: Hide');
  389. }
  390. else {
  391. $summary[] = t('Content image style: Original image');
  392. }
  393. if (isset($image_styles[$settings['colorbox_node_style_first']])) {
  394. $summary[] = t('Content image style of first image: @style', array('@style' => $image_styles[$settings['colorbox_node_style_first']]));
  395. }
  396. if (isset($image_styles[$settings['colorbox_image_style']])) {
  397. $summary[] = t('Colorbox image style: @style', array('@style' => $image_styles[$settings['colorbox_image_style']]));
  398. }
  399. else {
  400. $summary[] = t('Colorbox image style: Original image');
  401. }
  402. $gallery = array(
  403. 'post' => t('Per post gallery'),
  404. 'page' => t('Per page gallery'),
  405. 'field_post' => t('Per field in post gallery'),
  406. 'field_page' => t('Per field in page gallery'),
  407. 'custom' => t('Custom'),
  408. 'none' => t('No gallery'),
  409. );
  410. if (isset($settings['colorbox_gallery'])) {
  411. $summary[] = t('Colorbox gallery type: @type', array('@type' => $gallery[$settings['colorbox_gallery']])) . ($settings['colorbox_gallery'] == 'custom' ? ' (' . $settings['colorbox_gallery_custom'] . ')' : '');
  412. }
  413. $caption = array(
  414. 'auto' => t('Automatic'),
  415. 'title' => t('Title text'),
  416. 'alt' => t('Alt text'),
  417. 'node_title' => t('Content title'),
  418. 'custom' => t('Custom (with tokens)'),
  419. 'none' => t('None'),
  420. );
  421. if (isset($settings['colorbox_caption'])) {
  422. $summary[] = t('Colorbox caption: @type', array('@type' => $caption[$settings['colorbox_caption']]));
  423. }
  424. return implode('<br />', $summary);
  425. }
  426. /**
  427. * Implements hook_field_formatter_view().
  428. */
  429. function colorbox_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  430. $element = array();
  431. $index = $display['settings']['colorbox_multivalue_index'];
  432. foreach ($items as $delta => $item) {
  433. if ($index === NULL || $index === $delta) {
  434. $element[$delta] = array(
  435. '#theme' => 'colorbox_image_formatter',
  436. '#item' => $item,
  437. '#entity_type' => $entity_type,
  438. '#entity' => $entity,
  439. '#node' => $entity, // Left for legacy support.
  440. '#field' => $field,
  441. '#display_settings' => $display['settings'],
  442. '#delta' => $delta,
  443. );
  444. }
  445. }
  446. return $element;
  447. }
  448. /**
  449. * Implements hook_insert_styles().
  450. */
  451. function colorbox_insert_styles() {
  452. $insert_styles = array();
  453. foreach (image_styles() as $key => $style) {
  454. $label = isset($style['label']) ? $style['label'] : $style['name'];
  455. $insert_styles['colorbox__' . $key] = array('label' => t('Colorbox @style', array('@style' => $label)));
  456. }
  457. return $insert_styles;
  458. }
  459. /**
  460. * Implements hook_insert_content().
  461. */
  462. function colorbox_insert_content($item, $style, $widget) {
  463. list($item['module_name'], $item['style_name']) = explode('__', $style['name'], 2);
  464. return theme(array('colorbox_insert_image__' . str_replace('-', '_', $item['style_name']), 'colorbox_insert_image'), array('item' => $item, 'widget' => $widget));
  465. }
  466. /**
  467. * Machine names normally need to be unique but that does not apply to galleries.
  468. *
  469. * @return
  470. * Always FALSE
  471. */
  472. function colorbox_gallery_exists() {
  473. return FALSE;
  474. }