colorbox.theme.inc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * @file
  4. * Colorbox theme functions.
  5. */
  6. /**
  7. * Returns HTML for an Colorbox image field formatter.
  8. *
  9. * @param $variables
  10. * An associative array containing:
  11. * - item: An array of image data.
  12. * - image_style: An optional image style.
  13. * - path: An array containing the link 'path' and link 'options'.
  14. *
  15. * @ingroup themeable
  16. */
  17. function theme_colorbox_image_formatter($variables) {
  18. $item = $variables['item'];
  19. $entity_type = $variables['entity_type'];
  20. $entity = $variables['entity'];
  21. $field = $variables['field'];
  22. $settings = $variables['display_settings'];
  23. $image = array(
  24. 'path' => $item['uri'],
  25. 'alt' => $item['alt'],
  26. 'title' => $item['title'],
  27. 'style_name' => $settings['colorbox_node_style'],
  28. );
  29. if (isset($item['width']) && isset($item['height'])) {
  30. $image['width'] = $item['width'];
  31. $image['height'] = $item['height'];
  32. }
  33. $entity_title = entity_label($entity_type, $entity);
  34. switch ($settings['colorbox_caption']) {
  35. case 'auto':
  36. // If the title is empty use alt or the entity title in that order.
  37. if (!empty($image['title'])) {
  38. $caption = $image['title'];
  39. }
  40. elseif (!empty($image['alt'])) {
  41. $caption = $image['alt'];
  42. }
  43. elseif (!empty($entity_title)) {
  44. $caption = $entity_title;
  45. }
  46. else {
  47. $caption = '';
  48. }
  49. break;
  50. case 'title':
  51. $caption = $image['title'];
  52. break;
  53. case 'alt':
  54. $caption = $image['alt'];
  55. break;
  56. case 'node_title':
  57. $caption = $entity_title;
  58. break;
  59. case 'custom':
  60. $caption = token_replace($settings['colorbox_caption_custom'], array($entity_type => $entity, 'file' => (object) $item), array('clear' => TRUE));
  61. break;
  62. default:
  63. $caption = '';
  64. }
  65. // Shorten the caption for the example styles or when caption shortening is active.
  66. $colorbox_style = variable_get('colorbox_style', 'default');
  67. $trim_length = variable_get('colorbox_caption_trim_length', 75);
  68. if (((strpos($colorbox_style, 'colorbox/example') !== FALSE) || variable_get('colorbox_caption_trim', 0)) && (drupal_strlen($caption) > $trim_length)) {
  69. $caption = drupal_substr($caption, 0, $trim_length - 5) . '...';
  70. }
  71. // Build the gallery id.
  72. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  73. $entity_id = !empty($id) ? $entity_type . '-' . $id : 'entity-id';
  74. switch ($settings['colorbox_gallery']) {
  75. case 'post':
  76. $gallery_id = 'gallery-' . $entity_id;
  77. break;
  78. case 'page':
  79. $gallery_id = 'gallery-all';
  80. break;
  81. case 'field_post':
  82. $gallery_id = 'gallery-' . $entity_id . '-' . $field['field_name'];
  83. break;
  84. case 'field_page':
  85. $gallery_id = 'gallery-' . $field['field_name'];
  86. break;
  87. case 'custom':
  88. $gallery_id = $settings['colorbox_gallery_custom'];
  89. break;
  90. default:
  91. $gallery_id = '';
  92. }
  93. if ($style_name = $settings['colorbox_image_style']) {
  94. $path = image_style_url($style_name, $image['path']);
  95. }
  96. else {
  97. $path = file_create_url($image['path']);
  98. }
  99. return theme('colorbox_imagefield', array('image' => $image, 'path' => $path, 'title' => $caption, 'gid' => $gallery_id));
  100. }
  101. /**
  102. * Returns HTML for an image using a specific Colorbox image style.
  103. *
  104. * @param $variables
  105. * An associative array containing:
  106. * - image: image item as array.
  107. * - path: The path of the image that should be displayed in the Colorbox.
  108. * - title: The title text that will be used as a caption in the Colorbox.
  109. * - gid: Gallery id for Colorbox image grouping.
  110. *
  111. * @ingroup themeable
  112. */
  113. function theme_colorbox_imagefield($variables) {
  114. $class = array('colorbox');
  115. if ($variables['image']['style_name'] == 'hide') {
  116. $image = '';
  117. $class[] = 'js-hide';
  118. }
  119. elseif (!empty($variables['image']['style_name'])) {
  120. $image = theme('image_style', $variables['image']);
  121. }
  122. else {
  123. $image = theme('image', $variables['image']);
  124. }
  125. $options = array(
  126. 'html' => TRUE,
  127. 'attributes' => array(
  128. 'title' => $variables['title'],
  129. 'class' => $class,
  130. 'rel' => $variables['gid'],
  131. )
  132. );
  133. return l($image, $variables['path'], $options);
  134. }
  135. /**
  136. * Preprocess variables for the colorbox-insert-image.tpl.php file.
  137. */
  138. function template_preprocess_colorbox_insert_image(&$variables) {
  139. $class = array();
  140. $file = file_load($variables['item']['fid']);
  141. if (!empty($variables['widget']['settings']['insert_class'])) {
  142. $class = explode(' ', $variables['widget']['settings']['insert_class']);
  143. }
  144. $class[] = 'image-' . $variables['item']['style_name'];
  145. foreach ($class as $key => $value) {
  146. $class[$key] = drupal_html_class($value);
  147. }
  148. $variables['image_path'] = image_style_url($variables['item']['style_name'], $file->uri);
  149. if ($style_name = variable_get('colorbox_image_style', '')) {
  150. $variables['link_path'] = image_style_url($style_name, $file->uri);
  151. }
  152. else {
  153. $variables['link_path'] = file_create_url($file->uri);
  154. }
  155. $variables['class'] = implode(' ', $class);
  156. $variables['gallery_id'] = '';
  157. switch (variable_get('colorbox_insert_gallery', 0)) {
  158. case 0:
  159. case 1:
  160. case 2:
  161. $variables['gallery_id'] = 'gallery-all';
  162. break;
  163. case 3:
  164. $variables['gallery_id'] = '';
  165. break;
  166. }
  167. }