colorbox.theme.inc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /**
  3. * @file
  4. * Colorbox theme functions.
  5. */
  6. /**
  7. * Returns HTML for an Colorbox image field formatter.
  8. *
  9. * @param array $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. * @return string
  16. * An HTML string representing the themed output.
  17. *
  18. * @ingroup themeable
  19. */
  20. function theme_colorbox_image_formatter($variables) {
  21. static $gallery_token = NULL;
  22. $item = $variables['item'];
  23. $entity_type = $variables['entity_type'];
  24. $entity = $variables['entity'];
  25. $field = $variables['field'];
  26. $settings = $variables['display_settings'];
  27. $image = array(
  28. 'path' => $item['uri'],
  29. 'alt' => isset($item['alt']) ? $item['alt'] : '',
  30. 'title' => isset($item['title']) ? $item['title'] : '',
  31. 'style_name' => $settings['colorbox_node_style'],
  32. );
  33. if ($variables['delta'] == 0 && !empty($settings['colorbox_node_style_first'])) {
  34. $image['style_name'] = $settings['colorbox_node_style_first'];
  35. }
  36. if (isset($item['width']) && isset($item['height'])) {
  37. $image['width'] = $item['width'];
  38. $image['height'] = $item['height'];
  39. }
  40. if (isset($item['attributes'])) {
  41. $image['attributes'] = $item['attributes'];
  42. }
  43. // Allow image attributes to be overridden.
  44. if (isset($variables['item']['override']['attributes'])) {
  45. foreach (array('width', 'height', 'alt', 'title') as $key) {
  46. if (isset($variables['item']['override']['attributes'][$key])) {
  47. $image[$key] = $variables['item']['override']['attributes'][$key];
  48. unset($variables['item']['override']['attributes'][$key]);
  49. }
  50. }
  51. if (isset($image['attributes'])) {
  52. $image['attributes'] = $variables['item']['override']['attributes'] + $image['attributes'];
  53. }
  54. else {
  55. $image['attributes'] = $variables['item']['override']['attributes'];
  56. }
  57. }
  58. $entity_title = entity_label($entity_type, $entity);
  59. switch ($settings['colorbox_caption']) {
  60. case 'auto':
  61. // If the title is empty use alt or the entity title in that order.
  62. if (!empty($image['title'])) {
  63. $caption = $image['title'];
  64. }
  65. elseif (!empty($image['alt'])) {
  66. $caption = $image['alt'];
  67. }
  68. elseif (!empty($entity_title)) {
  69. $caption = $entity_title;
  70. }
  71. else {
  72. $caption = '';
  73. }
  74. break;
  75. case 'title':
  76. $caption = $image['title'];
  77. break;
  78. case 'alt':
  79. $caption = $image['alt'];
  80. break;
  81. case 'node_title':
  82. $caption = $entity_title;
  83. break;
  84. case 'custom':
  85. $caption = token_replace($settings['colorbox_caption_custom'], array($entity_type => $entity, 'file' => (object) $item), array('clear' => TRUE));
  86. break;
  87. default:
  88. $caption = '';
  89. }
  90. // Shorten the caption for the example styles or when caption shortening is active.
  91. $colorbox_style = variable_get('colorbox_style', 'default');
  92. $trim_length = variable_get('colorbox_caption_trim_length', 75);
  93. if (((strpos($colorbox_style, 'colorbox/example') !== FALSE) || variable_get('colorbox_caption_trim', 0)) && (drupal_strlen($caption) > $trim_length)) {
  94. $caption = drupal_substr($caption, 0, $trim_length - 5) . '...';
  95. }
  96. // Build the gallery id.
  97. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  98. $entity_id = !empty($id) ? $entity_type . '-' . $id : 'entity-id';
  99. switch ($settings['colorbox_gallery']) {
  100. case 'post':
  101. $gallery_id = 'gallery-' . $entity_id;
  102. break;
  103. case 'page':
  104. $gallery_id = 'gallery-all';
  105. break;
  106. case 'field_post':
  107. $gallery_id = 'gallery-' . $entity_id . '-' . $field['field_name'];
  108. break;
  109. case 'field_page':
  110. $gallery_id = 'gallery-' . $field['field_name'];
  111. break;
  112. case 'custom':
  113. $gallery_id = $settings['colorbox_gallery_custom'];
  114. break;
  115. default:
  116. $gallery_id = '';
  117. }
  118. // If gallery id is not empty add unique per-request token to avoid images being added manually to galleries.
  119. if (!empty($gallery_id) && variable_get('colorbox_unique_token', 1)) {
  120. // Check if gallery token has already been set, we need to reuse the token for the whole request.
  121. if (is_null($gallery_token)) {
  122. // We use a short token since randomness is not critical.
  123. $gallery_token = drupal_random_key(8);
  124. }
  125. $gallery_id = $gallery_id . '-' . $gallery_token;
  126. }
  127. if ($style_name = $settings['colorbox_image_style']) {
  128. $path = image_style_url($style_name, $image['path']);
  129. }
  130. else {
  131. $path = file_create_url($image['path']);
  132. }
  133. return theme('colorbox_imagefield', array('image' => $image, 'path' => $path, 'title' => $caption, 'gid' => $gallery_id));
  134. }
  135. /**
  136. * Returns HTML for an image using a specific Colorbox image style.
  137. *
  138. * @param array $variables
  139. * An associative array containing:
  140. * - image: image item as array.
  141. * - path: The path of the image that should be displayed in the Colorbox.
  142. * - title: The title text that will be used as a caption in the Colorbox.
  143. * - gid: Gallery id for Colorbox image grouping.
  144. *
  145. * @return string
  146. * An HTML string containing a link to the given path.
  147. *
  148. * @ingroup themeable
  149. */
  150. function theme_colorbox_imagefield($variables) {
  151. $class = array('colorbox');
  152. if ($variables['image']['style_name'] == 'hide') {
  153. $image = '';
  154. $class[] = 'js-hide';
  155. }
  156. elseif (!empty($variables['image']['style_name'])) {
  157. $image = theme('image_style', $variables['image']);
  158. }
  159. else {
  160. $image = theme('image', $variables['image']);
  161. }
  162. $options = drupal_parse_url($variables['path']);
  163. $options += array(
  164. 'html' => TRUE,
  165. 'attributes' => array(
  166. 'title' => $variables['title'],
  167. 'class' => $class,
  168. 'data-colorbox-gallery' => $variables['gid'],
  169. ),
  170. );
  171. return l($image, $options['path'], $options);
  172. }
  173. /**
  174. * Preprocess variables for the colorbox-insert-image.tpl.php file.
  175. *
  176. * @param array $variables
  177. */
  178. function template_preprocess_colorbox_insert_image(&$variables) {
  179. $item = $variables['item'];
  180. $variables['file'] = file_load($item['fid']);
  181. $variables['style_name'] = $item['style_name'];
  182. $variables['width'] = isset($item['width']) ? $item['width'] : NULL;
  183. $variables['height'] = isset($item['height']) ? $item['height'] : NULL;
  184. // Determine dimensions of the image after the image style transformations.
  185. image_style_transform_dimensions($variables['style_name'], $variables);
  186. $class = array();
  187. if (!empty($variables['widget']['settings']['insert_class'])) {
  188. $class = explode(' ', $variables['widget']['settings']['insert_class']);
  189. }
  190. $class[] = 'image-' . $variables['style_name'];
  191. foreach ($class as $key => $value) {
  192. $class[$key] = drupal_html_class($value);
  193. }
  194. $variables['class'] = implode(' ', $class);
  195. $variables['uri'] = image_style_path($variables['style_name'], $variables['file']->uri);
  196. $absolute = isset($variables['widget']['settings']['insert_absolute']) ? $variables['widget']['settings']['insert_absolute'] : NULL;
  197. $variables['url'] = insert_create_url($variables['uri'], $absolute, variable_get('clean_url'));
  198. // http://drupal.org/node/1923336
  199. if (function_exists('image_style_path_token')) {
  200. $token_query = array(IMAGE_DERIVATIVE_TOKEN => image_style_path_token($variables['style_name'], $variables['file']->uri));
  201. $variables['url'] .= (strpos($variables['url'], '?') !== FALSE ? '&' : '?') . drupal_http_build_query($token_query);
  202. }
  203. if ($style_name = variable_get('colorbox_image_style', '')) {
  204. $variables['path'] = image_style_url($style_name, $variables['file']->uri);
  205. }
  206. else {
  207. $variables['path'] = file_create_url($variables['file']->uri);
  208. }
  209. $variables['gallery_id'] = '';
  210. switch (variable_get('colorbox_insert_gallery', 0)) {
  211. case 0:
  212. case 1:
  213. case 2:
  214. $variables['gallery_id'] = 'gallery-all';
  215. break;
  216. case 3:
  217. $variables['gallery_id'] = '';
  218. break;
  219. }
  220. }