colorbox.theme.inc 7.2 KB

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