colorbox.inc 5.05 KB
Newer Older
1 2 3 4 5 6 7
<?php

/**
 * @file
 * Colorbox theme functions.
 */

frjo's avatar
frjo committed
8
use Drupal\Core\Template\Attribute;
9
use Drupal\Component\Utility\Crypt;
frjo's avatar
frjo committed
10
use Drupal\Component\Utility\Unicode;
frjo's avatar
frjo committed
11

12
/**
frjo's avatar
frjo committed
13
 * Prepares variables for colorbox formatter templates.
14
 *
frjo's avatar
frjo committed
15
 * Default template: colorbox-formatter.html.twig.
16
 *
frjo's avatar
frjo committed
17 18 19 20 21 22 23
 * @param array $variables
 *   An associative array containing:
 *   - item: An ImageItem object.
 *   - item_attributes: An optional associative array of html attributes to be
 *     placed in the img tag.
 *   - entity: An entity object.
 *   - settings: Formatter settings array.
24
 */
frjo's avatar
frjo committed
25
function template_preprocess_colorbox_formatter(&$variables) {
26 27
  static $gallery_token = NULL;

28
  $item = $variables['item'];
frjo's avatar
frjo committed
29 30
  $item_attributes = isset($variables['item_attributes']) ? $variables['item_attributes'] : array();

31 32
  $entity = $variables['entity'];
  $settings = $variables['settings'];
frjo's avatar
frjo committed
33 34 35
  $variables = array(); // Reset the $variables parameter.

  // Build the caption.
36 37 38 39
  $entity_title = $entity->label();
  $entity_type = $entity->bundle();

  switch ($settings['colorbox_caption']) {
40
     case 'auto':
41
      // If the title is empty use alt or the entity title in that order.
42 43
      if (!empty($item->title)) {
        $caption = $item->title;
44
      }
45 46
      elseif (!empty($item->alt)) {
        $caption = $item->alt;
47
      }
48
      elseif (!empty($entity_title)) {
49
        $caption = $entity_title;
50 51 52
      }
      else {
        $caption = '';
53 54 55
      }
      break;
    case 'title':
56
      $caption = $item->title;
57 58
      break;
    case 'alt':
59
      $caption = $item->alt;
60
      break;
61
    case 'entity_title':
62
      $caption = $entity_title;
63
      break;
64
    case 'custom':
65
      $token_service = \Drupal::token();
66
      $caption = $token_service->replace($settings['colorbox_caption_custom'], array($entity_type => $entity, 'file' => $item), array('clear' => TRUE));
67
      break;
68 69
    default:
      $caption = '';
70
  }
71

72
  // Shorten the caption for the example styles or when caption shortening is active.
73 74 75
  $config = \Drupal::config('colorbox.settings');
  $colorbox_style = $config->get('colorbox_style');
  $trim_length = $config->get('colorbox_caption_trim_length');
frjo's avatar
frjo committed
76
  if (((strpos($colorbox_style, 'colorbox/example') !== FALSE) || $config->get('colorbox_caption_trim')) && (Unicode::strlen($caption) > $trim_length)) {
77
    $caption = Unicode::substr($caption, 0, $trim_length - 5) . '...';
78
  }
79

80
  // Build the gallery id.
frjo's avatar
frjo committed
81 82 83 84
  $id = $entity->id();
  $entity_id = !empty($id) ? $entity_type . '-' . $id : 'entity-id';
  $field_name = $item->getParent()->getName();

85
  switch ($settings['colorbox_gallery']) {
86
    case 'post':
87
      $gallery_id = 'gallery-' . $entity_id;
88
      break;
89 90 91 92
    case 'page':
      $gallery_id = 'gallery-all';
      break;
    case 'field_post':
93
      $gallery_id = 'gallery-' . $entity_id . '-' . $field_name;
94
      break;
95
    case 'field_page':
96
      $gallery_id = 'gallery-' . $field_name;
97 98
      break;
    case 'custom':
grisendo's avatar
grisendo committed
99
      $token_service = \Drupal::token();
100
      $gallery_id = $token_service->replace($settings['colorbox_gallery_custom'], array($entity_type => $entity, 'file' => $item), array('clear' => TRUE));
101
      break;
102 103
    default:
      $gallery_id = '';
104
  }
105

106 107 108 109 110 111 112 113 114 115
  // If gallery id is not empty add unique per-request token to avoid images being added manually to galleries.
  if (!empty($gallery_id) && $config->get('advanced.unique_token')) {
    // Check if gallery token has already been set, we need to reuse the token for the whole request.
    if (is_null($gallery_token)) {
      // We use a short token since randomness is not critical.
      $gallery_token = Crypt::randomBytesBase64(8);
    }
    $gallery_id = $gallery_id . '-' . $gallery_token;
  }

116 117 118 119 120 121 122 123
  // Set up the $variables['image'] parameter.
  if ($settings['style_first']) {
    $variables['image'] = array(
      '#theme' => 'image_style',
      '#style_name' => $settings['colorbox_node_style_first'],
    );
  }
  elseif (!empty($settings['colorbox_node_style'])) {
frjo's avatar
frjo committed
124 125 126 127 128 129 130 131 132 133 134 135 136
    $variables['image'] = array(
      '#theme' => 'image_style',
      '#style_name' => $settings['colorbox_node_style'],
    );
  }
  else {
    $variables['image'] = array(
      '#theme' => 'image',
    );
  }
  $variables['image']['#attributes'] = $item_attributes;

  // Do not output an empty 'title' attribute.
frjo's avatar
frjo committed
137
  if (Unicode::strlen($item->title) != 0) {
frjo's avatar
frjo committed
138 139 140 141 142 143 144 145 146 147
    $variables['image']['#title'] = $item->title;
  }

  foreach (array('width', 'height', 'alt') as $key) {
    $variables['image']["#$key"] = $item->$key;
  }

  $image_uri = $item->entity->getFileUri();
  $variables['image']['#uri'] = empty($item->uri) ? $image_uri : $item->uri;

148
  if (!empty($settings['colorbox_image_style'])) {
149
    $style = entity_load('image_style', $settings['colorbox_image_style']);
frjo's avatar
frjo committed
150
    $variables['url'] = $style->buildUrl($image_uri);
151 152
  }
  else {
frjo's avatar
frjo committed
153
    $variables['url'] = file_create_url($image_uri);
154 155
  }

156
  $class = array('colorbox');
157
  if ($settings['colorbox_node_style'] == 'hide') {
158 159
    $class[] = 'js-hide';
  }
frjo's avatar
frjo committed
160 161 162 163
  $variables['attributes'] = new Attribute();
  $variables['attributes']['title'] = $caption;
  $variables['attributes']['rel'] = $gallery_id;
  $variables['attributes']['class'] = $class;
164
}