colorbox.theme.inc 7.6 KB

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