Projet

Général

Profil

Paste
Télécharger (8,32 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / colorbox / colorbox.theme.inc @ b433176d

1
<?php
2

    
3
/**
4
 * @file
5
 * Colorbox theme functions.
6
 */
7

    
8
/**
9
 * Returns HTML for an Colorbox image field formatter.
10
 *
11
 * @param array $variables
12
 *   An associative array containing:
13
 *   - item: An array of image data.
14
 *   - image_style: An optional image style.
15
 *   - path: An array containing the link 'path' and link 'options'.
16
 *
17
 * @return string
18
 *   An HTML string representing the themed output.
19
 *
20
 * @ingroup themeable
21
 *
22
 * @codingStandardsIgnoreStart
23
 */
24
function theme_colorbox_image_formatter($variables) {
25
  // @codingStandardsIgnoreEnd
26
  global $language;
27
  static $gallery_token = NULL;
28
  $item = $variables['item'];
29
  $entity_type = $variables['entity_type'];
30
  $entity = $variables['entity'];
31
  $field = $variables['field'];
32
  $settings = $variables['display_settings'];
33

    
34
  $image = array(
35
    'path' => $item['uri'],
36
    'alt' => isset($item['alt']) ? $item['alt'] : '',
37
    'title' => isset($item['title']) ? $item['title'] : '',
38
    'style_name' => $settings['colorbox_node_style'],
39
  );
40

    
41
  if ($variables['delta'] == 0 && !empty($settings['colorbox_node_style_first'])) {
42
    $image['style_name'] = $settings['colorbox_node_style_first'];
43
  }
44

    
45
  if (isset($item['width']) && isset($item['height'])) {
46
    $image['width'] = $item['width'];
47
    $image['height'] = $item['height'];
48
  }
49

    
50
  if (isset($item['attributes'])) {
51
    $image['attributes'] = $item['attributes'];
52
  }
53

    
54
  // Allow image attributes to be overridden.
55
  if (isset($variables['item']['override']['attributes'])) {
56
    foreach (array('width', 'height', 'alt', 'title') as $key) {
57
      if (isset($variables['item']['override']['attributes'][$key])) {
58
        $image[$key] = $variables['item']['override']['attributes'][$key];
59
        unset($variables['item']['override']['attributes'][$key]);
60
      }
61
    }
62
    if (isset($image['attributes'])) {
63
      $image['attributes'] = $variables['item']['override']['attributes'] + $image['attributes'];
64
    }
65
    else {
66
      $image['attributes'] = $variables['item']['override']['attributes'];
67
    }
68
  }
69

    
70
  $entity_title = entity_label($entity_type, $entity);
71

    
72
  switch ($settings['colorbox_caption']) {
73
    case 'auto':
74
      // If the title is empty use alt or the entity title in that order.
75
      if (!empty($image['title'])) {
76
        $caption = $image['title'];
77
      }
78
      elseif (!empty($image['alt'])) {
79
        $caption = $image['alt'];
80
      }
81
      elseif (!empty($entity_title)) {
82
        $caption = $entity_title;
83
      }
84
      else {
85
        $caption = '';
86
      }
87
      break;
88

    
89
    case 'title':
90
      $caption = $image['title'];
91
      break;
92

    
93
    case 'alt':
94
      $caption = $image['alt'];
95
      break;
96

    
97
    case 'node_title':
98
      $caption = $entity_title;
99
      break;
100

    
101
    case 'custom':
102
      $caption = token_replace($settings['colorbox_caption_custom'], array($entity_type => $entity, 'file' => (object) $item), array('clear' => TRUE, 'language' => $language));
103
      break;
104

    
105
    default:
106
      $caption = '';
107
  }
108

    
109
  // Shorten the caption for the example styles or when caption shortening is
110
  // active.
111
  $colorbox_style = variable_get('colorbox_style', 'default');
112
  $trim_length = variable_get('colorbox_caption_trim_length', 75);
113
  if (((strpos($colorbox_style, 'colorbox/example') !== FALSE) || variable_get('colorbox_caption_trim', 0)) && (drupal_strlen($caption) > $trim_length)) {
114
    $caption = drupal_substr($caption, 0, $trim_length - 5) . '...';
115
  }
116

    
117
  // Build the gallery id.
118
  list($id) = entity_extract_ids($entity_type, $entity);
119
  $entity_id = !empty($id) ? $entity_type . '-' . $id : 'entity-id';
120
  switch ($settings['colorbox_gallery']) {
121
    case 'post':
122
      $gallery_id = 'gallery-' . $entity_id;
123
      break;
124

    
125
    case 'page':
126
      $gallery_id = 'gallery-all';
127
      break;
128

    
129
    case 'field_post':
130
      $gallery_id = 'gallery-' . $entity_id . '-' . $field['field_name'];
131
      break;
132

    
133
    case 'field_page':
134
      $gallery_id = isset($field['field_name']) ? 'gallery-' . $field['field_name'] : 'gallery-page';
135
      break;
136

    
137
    case 'custom':
138
      $gallery_id = $settings['colorbox_gallery_custom'];
139
      break;
140

    
141
    default:
142
      $gallery_id = '';
143
  }
144

    
145
  // If gallery id is not empty add unique per-request token to avoid images
146
  // being added manually to galleries.
147
  if (!empty($gallery_id) && variable_get('colorbox_unique_token', 1)) {
148
    // Check if gallery token has already been set, we need to reuse the token
149
    // for the whole request.
150
    if (is_null($gallery_token)) {
151
      // We use a short token since randomness is not critical.
152
      $gallery_token = drupal_random_key(8);
153
    }
154
    $gallery_id = $gallery_id . '-' . $gallery_token;
155
  }
156

    
157
  if ($style_name = $settings['colorbox_image_style']) {
158
    $path = image_style_url($style_name, $image['path']);
159
  }
160
  else {
161
    $path = file_create_url($image['path']);
162
  }
163

    
164
  return theme('colorbox_imagefield', array(
165
    'image' => $image,
166
    'path' => $path,
167
    'title' => $caption,
168
    'gid' => $gallery_id,
169
  ));
170
}
171

    
172
/**
173
 * Returns HTML for an image using a specific Colorbox image style.
174
 *
175
 * @param array $variables
176
 *   An associative array containing:
177
 *   - image: image item as array.
178
 *   - path: The path of the image that should be displayed in the Colorbox.
179
 *   - title: The title text that will be used as a caption in the Colorbox.
180
 *   - gid: Gallery id for Colorbox image grouping.
181
 *
182
 * @return string
183
 *   An HTML string containing a link to the given path.
184
 *
185
 * @ingroup themeable
186
 *
187
 * @codingStandardsIgnoreStart
188
 */
189
function theme_colorbox_imagefield($variables) {
190
  // @codingStandardsIgnoreEnd
191
  $class = array('colorbox');
192

    
193
  if ($variables['image']['style_name'] == 'hide') {
194
    $image = '';
195
    $class[] = 'js-hide';
196
  }
197
  elseif (!empty($variables['image']['style_name'])) {
198
    $image = theme('image_style', $variables['image']);
199
  }
200
  else {
201
    $image = theme('image', $variables['image']);
202
  }
203

    
204
  $options = drupal_parse_url($variables['path']);
205
  $options += array(
206
    'html' => TRUE,
207
    'attributes' => array(
208
      'title' => $variables['title'],
209
      'class' => $class,
210
      'data-colorbox-gallery' => $variables['gid'],
211
      'data-cbox-img-attrs' => '{"title": "' . $variables['image']['title'] . '", "alt": "' . $variables['image']['alt'] . '"}',
212
    ),
213
  );
214

    
215
  return l($image, $options['path'], $options);
216
}
217

    
218
/**
219
 * Preprocess variables for the colorbox-insert-image.tpl.php file.
220
 *
221
 * @param array $variables
222
 *   Array with variables values.
223
 *
224
 * @codingStandardsIgnoreStart
225
 */
226
function template_preprocess_colorbox_insert_image(&$variables) {
227
  // @codingStandardsIgnoreEnd
228
  $item = $variables['item'];
229
  $variables['file'] = file_load($item['fid']);
230
  $variables['style_name'] = $item['style_name'];
231
  $variables['width'] = isset($item['width']) ? $item['width'] : NULL;
232
  $variables['height'] = isset($item['height']) ? $item['height'] : NULL;
233

    
234
  // Determine dimensions of the image after the image style transformations.
235
  image_style_transform_dimensions($variables['style_name'], $variables);
236

    
237
  $class = array();
238
  if (!empty($variables['widget']['settings']['insert_class'])) {
239
    $class = explode(' ', $variables['widget']['settings']['insert_class']);
240
  }
241
  $class[] = 'image-' . $variables['style_name'];
242

    
243
  foreach ($class as $key => $value) {
244
    $class[$key] = drupal_html_class($value);
245
  }
246

    
247
  $variables['class'] = implode(' ', $class);
248

    
249
  $variables['uri'] = image_style_path($variables['style_name'], $variables['file']->uri);
250
  $absolute = isset($variables['widget']['settings']['insert_absolute']) ? $variables['widget']['settings']['insert_absolute'] : NULL;
251
  $variables['url'] = insert_create_url($variables['uri'], $absolute, variable_get('clean_url'));
252

    
253
  // http://drupal.org/node/1923336
254
  if (function_exists('image_style_path_token')) {
255
    $token_query = array(IMAGE_DERIVATIVE_TOKEN => image_style_path_token($variables['style_name'], $variables['file']->uri));
256
    $variables['url'] .= (strpos($variables['url'], '?') !== FALSE ? '&' : '?') . drupal_http_build_query($token_query);
257
  }
258

    
259
  if ($style_name = variable_get('colorbox_image_style', '')) {
260
    $variables['path'] = image_style_url($style_name, $variables['file']->uri);
261
  }
262
  else {
263
    $variables['path'] = file_create_url($variables['file']->uri);
264
  }
265

    
266
  $variables['gallery_id'] = '';
267
  switch (variable_get('colorbox_insert_gallery', 0)) {
268
    case 0:
269
    case 1:
270
    case 2:
271
      $variables['gallery_id'] = 'gallery-all';
272
      break;
273

    
274
    case 3:
275
      $variables['gallery_id'] = '';
276
      break;
277
  }
278
}