Projet

Général

Profil

Paste
Télécharger (7,98 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / colorbox / colorbox.theme.inc @ 651307cd

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
function theme_colorbox_image_formatter($variables) {
23
  static $gallery_token = NULL;
24
  $item = $variables['item'];
25
  $entity_type = $variables['entity_type'];
26
  $entity = $variables['entity'];
27
  $field = $variables['field'];
28
  $settings = $variables['display_settings'];
29

    
30
  $image = array(
31
    'path' => $item['uri'],
32
    'alt' => isset($item['alt']) ? $item['alt'] : '',
33
    'title' => isset($item['title']) ? $item['title'] : '',
34
    'style_name' => $settings['colorbox_node_style'],
35
  );
36

    
37
  if ($variables['delta'] == 0 && !empty($settings['colorbox_node_style_first'])) {
38
    $image['style_name'] = $settings['colorbox_node_style_first'];
39
  }
40

    
41
  if (isset($item['width']) && isset($item['height'])) {
42
    $image['width'] = $item['width'];
43
    $image['height'] = $item['height'];
44
  }
45

    
46
  if (isset($item['attributes'])) {
47
    $image['attributes'] = $item['attributes'];
48
  }
49

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

    
66
  $entity_title = entity_label($entity_type, $entity);
67

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

    
85
    case 'title':
86
      $caption = $image['title'];
87
      break;
88

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

    
93
    case 'node_title':
94
      $caption = $entity_title;
95
      break;
96

    
97
    case 'custom':
98
      $caption = token_replace($settings['colorbox_caption_custom'], array($entity_type => $entity, 'file' => (object) $item), array('clear' => TRUE));
99
      break;
100

    
101
    default:
102
      $caption = '';
103
  }
104

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

    
112
  // Build the gallery id.
113
  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
114
  $entity_id = !empty($id) ? $entity_type . '-' . $id : 'entity-id';
115
  switch ($settings['colorbox_gallery']) {
116
    case 'post':
117
      $gallery_id = 'gallery-' . $entity_id;
118
      break;
119

    
120
    case 'page':
121
      $gallery_id = 'gallery-all';
122
      break;
123

    
124
    case 'field_post':
125
      $gallery_id = 'gallery-' . $entity_id . '-' . $field['field_name'];
126
      break;
127

    
128
    case 'field_page':
129
      $gallery_id = 'gallery-' . $field['field_name'];
130
      break;
131

    
132
    case 'custom':
133
      $gallery_id = $settings['colorbox_gallery_custom'];
134
      break;
135

    
136
    default:
137
      $gallery_id = '';
138
  }
139

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

    
150
  if ($style_name = $settings['colorbox_image_style']) {
151
    $path = image_style_url($style_name, $image['path']);
152
  }
153
  else {
154
    $path = file_create_url($image['path']);
155
  }
156

    
157
  return theme('colorbox_imagefield', array('image' => $image, 'path' => $path, 'title' => $caption, 'gid' => $gallery_id));
158
}
159

    
160
/**
161
 * Returns HTML for an image using a specific Colorbox image style.
162
 *
163
 * @param array $variables
164
 *   An associative array containing:
165
 *   - image: image item as array.
166
 *   - path: The path of the image that should be displayed in the Colorbox.
167
 *   - title: The title text that will be used as a caption in the Colorbox.
168
 *   - gid: Gallery id for Colorbox image grouping.
169
 *
170
 * @return string
171
 *   An HTML string containing a link to the given path.
172
 *
173
 * @ingroup themeable
174
 */
175
function theme_colorbox_imagefield($variables) {
176
  $class = array('colorbox');
177

    
178
  if ($variables['image']['style_name'] == 'hide') {
179
    $image = '';
180
    $class[] = 'js-hide';
181
  }
182
  elseif (!empty($variables['image']['style_name'])) {
183
    $image = theme('image_style', $variables['image']);
184
  }
185
  else {
186
    $image = theme('image', $variables['image']);
187
  }
188

    
189
  $options = drupal_parse_url($variables['path']);
190
  $options += array(
191
    'html' => TRUE,
192
    'attributes' => array(
193
      'title' => $variables['title'],
194
      'class' => $class,
195
      'data-colorbox-gallery' => $variables['gid'],
196
      'data-cbox-img-attrs' => '{"title": "' . $variables['image']['title'] . '", "alt": "' . $variables['image']['alt'] . '"}',
197
    ),
198
  );
199

    
200
  return l($image, $options['path'], $options);
201
}
202

    
203
/**
204
 * Preprocess variables for the colorbox-insert-image.tpl.php file.
205
 *
206
 * @param array $variables
207
 */
208
function template_preprocess_colorbox_insert_image(&$variables) {
209
  $item = $variables['item'];
210
  $variables['file'] = file_load($item['fid']);
211
  $variables['style_name'] = $item['style_name'];
212
  $variables['width'] = isset($item['width']) ? $item['width'] : NULL;
213
  $variables['height'] = isset($item['height']) ? $item['height'] : NULL;
214

    
215
  // Determine dimensions of the image after the image style transformations.
216
  image_style_transform_dimensions($variables['style_name'], $variables);
217

    
218
  $class = array();
219
  if (!empty($variables['widget']['settings']['insert_class'])) {
220
    $class = explode(' ', $variables['widget']['settings']['insert_class']);
221
  }
222
  $class[] = 'image-' . $variables['style_name'];
223

    
224
  foreach ($class as $key => $value) {
225
    $class[$key] = drupal_html_class($value);
226
  }
227

    
228
  $variables['class'] = implode(' ', $class);
229

    
230
  $variables['uri'] = image_style_path($variables['style_name'], $variables['file']->uri);
231
  $absolute = isset($variables['widget']['settings']['insert_absolute']) ? $variables['widget']['settings']['insert_absolute'] : NULL;
232
  $variables['url'] = insert_create_url($variables['uri'], $absolute, variable_get('clean_url'));
233

    
234
  // http://drupal.org/node/1923336
235
  if (function_exists('image_style_path_token')) {
236
    $token_query = array(IMAGE_DERIVATIVE_TOKEN => image_style_path_token($variables['style_name'], $variables['file']->uri));
237
    $variables['url'] .= (strpos($variables['url'], '?') !== FALSE ? '&' : '?') . drupal_http_build_query($token_query);
238
  }
239

    
240
  if ($style_name = variable_get('colorbox_image_style', '')) {
241
    $variables['path'] = image_style_url($style_name, $variables['file']->uri);
242
  }
243
  else {
244
    $variables['path'] = file_create_url($variables['file']->uri);
245
  }
246

    
247
  $variables['gallery_id'] = '';
248
  switch (variable_get('colorbox_insert_gallery', 0)) {
249
    case 0:
250
    case 1:
251
    case 2:
252
      $variables['gallery_id'] = 'gallery-all';
253
      break;
254

    
255
    case 3:
256
      $variables['gallery_id'] = '';
257
      break;
258
  }
259
}