Projet

Général

Profil

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

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

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
    case 'title':
85
      $caption = $image['title'];
86
      break;
87
    case 'alt':
88
      $caption = $image['alt'];
89
      break;
90
    case 'node_title':
91
      $caption = $entity_title;
92
      break;
93
    case 'custom':
94
      $caption = token_replace($settings['colorbox_caption_custom'], array($entity_type => $entity, 'file' => (object) $item), array('clear' => TRUE));
95
      break;
96
    default:
97
      $caption = '';
98
  }
99

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

    
107
  // Build the gallery id.
108
  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
109
  $entity_id = !empty($id) ? $entity_type . '-' . $id : 'entity-id';
110
  switch ($settings['colorbox_gallery']) {
111
    case 'post':
112
      $gallery_id = 'gallery-' . $entity_id;
113
      break;
114
    case 'page':
115
      $gallery_id = 'gallery-all';
116
      break;
117
    case 'field_post':
118
      $gallery_id = 'gallery-' . $entity_id . '-' . $field['field_name'];
119
      break;
120
    case 'field_page':
121
      $gallery_id = 'gallery-' . $field['field_name'];
122
      break;
123
    case 'custom':
124
      $gallery_id = $settings['colorbox_gallery_custom'];
125
      break;
126
    default:
127
      $gallery_id = '';
128
  }
129

    
130
  // If gallery id is not empty add unique per-request token to avoid images being added manually to galleries.
131
  if (!empty($gallery_id) && variable_get('colorbox_unique_token', 1)) {
132
    // Check if gallery token has already been set, we need to reuse the token for the whole request.
133
    if (is_null($gallery_token)) {
134
      // We use a short token since randomness is not critical.
135
      $gallery_token = drupal_random_key(8);
136
    }
137
    $gallery_id = $gallery_id . '-' . $gallery_token;
138
  }
139

    
140
  if ($style_name = $settings['colorbox_image_style']) {
141
    $path = image_style_url($style_name, $image['path']);
142
  }
143
  else {
144
    $path = file_create_url($image['path']);
145
  }
146

    
147
  return theme('colorbox_imagefield', array('image' => $image, 'path' => $path, 'title' => $caption, 'gid' => $gallery_id));
148
}
149

    
150
/**
151
 * Returns HTML for an image using a specific Colorbox image style.
152
 *
153
 * @param array $variables
154
 *   An associative array containing:
155
 *   - image: image item as array.
156
 *   - path: The path of the image that should be displayed in the Colorbox.
157
 *   - title: The title text that will be used as a caption in the Colorbox.
158
 *   - gid: Gallery id for Colorbox image grouping.
159
 *
160
 * @return string
161
 *   An HTML string containing a link to the given path.
162
 *
163
 * @ingroup themeable
164
 */
165
function theme_colorbox_imagefield($variables) {
166
  $class = array('colorbox');
167

    
168
  if ($variables['image']['style_name'] == 'hide') {
169
    $image = '';
170
    $class[] = 'js-hide';
171
  }
172
  elseif (!empty($variables['image']['style_name'])) {
173
    $image = theme('image_style', $variables['image']);
174
  }
175
  else {
176
    $image = theme('image', $variables['image']);
177
  }
178

    
179
  $options = drupal_parse_url($variables['path']);
180
  $options += array(
181
    'html' => TRUE,
182
    'attributes' => array(
183
      'title' => $variables['title'],
184
      'class' => $class,
185
      'data-colorbox-gallery' => $variables['gid'],
186
    ),
187
  );
188

    
189
  return l($image, $options['path'], $options);
190
}
191

    
192
/**
193
 * Preprocess variables for the colorbox-insert-image.tpl.php file.
194
 *
195
 * @param array $variables
196
 */
197
function template_preprocess_colorbox_insert_image(&$variables) {
198
  $item = $variables['item'];
199
  $variables['file'] = file_load($item['fid']);
200
  $variables['style_name'] = $item['style_name'];
201
  $variables['width'] = isset($item['width']) ? $item['width'] : NULL;
202
  $variables['height'] = isset($item['height']) ? $item['height'] : NULL;
203

    
204
  // Determine dimensions of the image after the image style transformations.
205
  image_style_transform_dimensions($variables['style_name'], $variables);
206

    
207
  $class = array();
208
  if (!empty($variables['widget']['settings']['insert_class'])) {
209
    $class = explode(' ', $variables['widget']['settings']['insert_class']);
210
  }
211
  $class[] = 'image-' . $variables['style_name'];
212

    
213
  foreach ($class as $key => $value) {
214
    $class[$key] = drupal_html_class($value);
215
  }
216

    
217
  $variables['class'] = implode(' ', $class);
218

    
219
  $variables['uri'] = image_style_path($variables['style_name'], $variables['file']->uri);
220
  $absolute = isset($variables['widget']['settings']['insert_absolute']) ? $variables['widget']['settings']['insert_absolute'] : NULL;
221
  $variables['url'] = insert_create_url($variables['uri'], $absolute, variable_get('clean_url'));
222

    
223
  // http://drupal.org/node/1923336
224
  if (function_exists('image_style_path_token')) {
225
    $token_query = array(IMAGE_DERIVATIVE_TOKEN => image_style_path_token($variables['style_name'], $variables['file']->uri));
226
    $variables['url'] .= (strpos($variables['url'], '?') !== FALSE ? '&' : '?') . drupal_http_build_query($token_query);
227
  }
228

    
229
  if ($style_name = variable_get('colorbox_image_style', '')) {
230
    $variables['path'] = image_style_url($style_name, $variables['file']->uri);
231
  }
232
  else {
233
    $variables['path'] = file_create_url($variables['file']->uri);
234
  }
235

    
236
  $variables['gallery_id'] = '';
237
  switch (variable_get('colorbox_insert_gallery', 0)) {
238
    case 0:
239
    case 1:
240
    case 2:
241
      $variables['gallery_id'] = 'gallery-all';
242
      break;
243
    case 3:
244
      $variables['gallery_id'] = '';
245
      break;
246
  }
247
}