colorbox.theme.inc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * @file
  4. * Colorbox theme functions.
  5. */
  6. use Drupal\Component\Utility\Crypt;
  7. use Drupal\Component\Utility\Unicode;
  8. use Drupal\file\Entity\File;
  9. use Drupal\image\Entity\ImageStyle;
  10. /**
  11. * Prepares variables for colorbox formatter templates.
  12. *
  13. * Default template: colorbox-formatter.html.twig.
  14. *
  15. * @param array $variables
  16. * An associative array containing:
  17. * - item: An ImageItem object.
  18. * - item_attributes: An optional associative array of html attributes to be
  19. * placed in the img tag.
  20. * - entity: An entity object.
  21. * - settings: Formatter settings array.
  22. */
  23. function template_preprocess_colorbox_formatter(&$variables) {
  24. static $gallery_token = NULL;
  25. $item = $variables['item'];
  26. $item_attributes = isset($variables['item_attributes']) ? $variables['item_attributes'] : [];
  27. $entity = $variables['entity'];
  28. $settings = $variables['settings'];
  29. $image_uri = $item->entity->getFileUri();
  30. $classes_array = ['colorbox'];
  31. $data_cbox_img_attrs = [];
  32. // Build the caption.
  33. $entity_title = $entity->label();
  34. $entity_type = $entity->getEntityTypeId();
  35. $entity_bundle = $entity->bundle();
  36. switch ($settings['colorbox_caption']) {
  37. case 'auto':
  38. // If the title is empty use alt or the entity title in that order.
  39. if (!empty($item->title)) {
  40. $caption = $item->title;
  41. }
  42. elseif (!empty($item->alt)) {
  43. $caption = $item->alt;
  44. }
  45. elseif (!empty($entity_title)) {
  46. $caption = $entity_title;
  47. }
  48. else {
  49. $caption = '';
  50. }
  51. break;
  52. case 'title':
  53. $caption = $item->title;
  54. break;
  55. case 'alt':
  56. $caption = $item->alt;
  57. break;
  58. case 'entity_title':
  59. $caption = $entity_title;
  60. break;
  61. case 'custom':
  62. $token_service = \Drupal::token();
  63. $caption = $token_service->replace($settings['colorbox_caption_custom'], [$entity_type => $entity, 'file' => $item], ['clear' => TRUE]);
  64. break;
  65. default:
  66. $caption = '';
  67. }
  68. // Shorten the caption for the example styles or when caption
  69. // shortening is active.
  70. $config = \Drupal::config('colorbox.settings');
  71. $colorbox_style = $config->get('colorbox_style');
  72. $trim_length = $config->get('colorbox_caption_trim_length');
  73. if (((strpos($colorbox_style, 'colorbox/example') !== FALSE) || $config->get('colorbox_caption_trim')) && (Unicode::strlen($caption) > $trim_length)) {
  74. $caption = Unicode::substr($caption, 0, $trim_length - 5) . '...';
  75. }
  76. // Build the gallery id.
  77. $id = $entity->id();
  78. $entity_id = !empty($id) ? $entity_bundle . '-' . $id : 'entity-id';
  79. $field_name = $item->getParent()->getName();
  80. switch ($settings['colorbox_gallery']) {
  81. case 'post':
  82. $gallery_id = 'gallery-' . $entity_id;
  83. break;
  84. case 'page':
  85. $gallery_id = 'gallery-all';
  86. break;
  87. case 'field_post':
  88. $gallery_id = 'gallery-' . $entity_id . '-' . $field_name;
  89. break;
  90. case 'field_page':
  91. $gallery_id = 'gallery-' . $field_name;
  92. break;
  93. case 'custom':
  94. $token_service = \Drupal::token();
  95. $gallery_id = $token_service->replace($settings['colorbox_gallery_custom'], [$entity_type => $entity, 'file' => $item], ['clear' => TRUE]);
  96. break;
  97. default:
  98. $gallery_id = '';
  99. }
  100. // If gallery id is not empty add unique per-request token to avoid
  101. // images being added manually to galleries.
  102. if (!empty($gallery_id) && $config->get('advanced.unique_token')) {
  103. // Check if gallery token has already been set, we need to reuse
  104. // the token for the whole request.
  105. if (is_null($gallery_token)) {
  106. // We use a short token since randomness is not critical.
  107. $gallery_token = Crypt::randomBytesBase64(8);
  108. }
  109. $gallery_id = $gallery_id . '-' . $gallery_token;
  110. }
  111. // Set up the $variables['image'] parameter.
  112. if ($settings['style_name'] == 'hide') {
  113. $variables['image'] = [];
  114. $classes_array[] = 'js-hide';
  115. }
  116. elseif (!empty($settings['style_name'])) {
  117. $variables['image'] = [
  118. '#theme' => 'image_style',
  119. '#style_name' => $settings['style_name'],
  120. ];
  121. }
  122. else {
  123. $variables['image'] = [
  124. '#theme' => 'image',
  125. ];
  126. }
  127. if (!empty($variables['image'])) {
  128. $variables['image']['#attributes'] = $item_attributes;
  129. // Do not output an empty 'title' attribute.
  130. if (Unicode::strlen($item->title) != 0) {
  131. $variables['image']['#title'] = $item->title;
  132. $data_cbox_img_attrs['title'] = '"title":"' . $item->title . '"';
  133. }
  134. foreach (['width', 'height', 'alt'] as $key) {
  135. $variables['image']["#$key"] = $item->$key;
  136. if ($key == 'alt') {
  137. $data_cbox_img_attrs['alt'] = '"alt":"' . $item->alt . '"';
  138. }
  139. }
  140. $variables['image']['#uri'] = empty($item->uri) ? $image_uri : $item->uri;
  141. }
  142. if (!empty($settings['colorbox_image_style'])) {
  143. $style = ImageStyle::load($settings['colorbox_image_style']);
  144. $variables['url'] = $style->buildUrl($image_uri);
  145. }
  146. else {
  147. $variables['url'] = file_create_url($image_uri);
  148. }
  149. // If File Entity module is enabled, load attribute values from file entity.
  150. if (\Drupal::moduleHandler()->moduleExists('file_entity')) {
  151. // File id of the save file.
  152. $fid = $item->target_id;
  153. // Load file object.
  154. $file_obj = File::load($fid);
  155. $file_array = $file_obj->toArray();
  156. // Populate the image title.
  157. if (!empty($file_array['field_image_title_text'][0]['value']) && empty($item->title) && $settings['colorbox_caption'] == 'title') {
  158. $caption = $file_array['field_image_title_text'][0]['value'];
  159. }
  160. // Populate the image alt text.
  161. if (!empty($file_array['field_image_alt_text'][0]['value']) && empty($item->alt) && $settings['colorbox_caption'] == 'alt') {
  162. $caption = $file_array['field_image_alt_text'][0]['value'];
  163. }
  164. }
  165. $variables['attributes']['title'] = $caption;
  166. $variables['attributes']['data-colorbox-gallery'] = $gallery_id;
  167. $variables['attributes']['class'] = $classes_array;
  168. if (!empty($data_cbox_img_attrs)) {
  169. $variables['attributes']['data-cbox-img-attrs'] = '{' . implode(',', $data_cbox_img_attrs) . '}';
  170. }
  171. }