colorbox.module 15 KB

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