Projet

Général

Profil

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

root / drupal7 / sites / all / modules / colorbox / colorbox.theme.inc @ 0ccfec7f

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 $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
 * @ingroup themeable
18
 */
19
function theme_colorbox_image_formatter($variables) {
20
  static $gallery_token = NULL;
21
  $item = $variables['item'];
22
  $entity_type = $variables['entity_type'];
23
  $entity = $variables['entity'];
24
  $field = $variables['field'];
25
  $settings = $variables['display_settings'];
26

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

    
34
  if ($variables['delta'] == 0 && !empty($settings['colorbox_node_style_first'])) {
35
    $image['style_name'] = $settings['colorbox_node_style_first'];
36
  }
37

    
38
  if (isset($item['width']) && isset($item['height'])) {
39
    $image['width'] = $item['width'];
40
    $image['height'] = $item['height'];
41
  }
42

    
43
  if (isset($item['attributes'])) {
44
    $image['attributes'] = $item['attributes'];
45
  }
46

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

    
63
  $entity_title = entity_label($entity_type, $entity);
64

    
65
  switch ($settings['colorbox_caption']) {
66
     case 'auto':
67
      // If the title is empty use alt or the entity title in that order.
68
      if (!empty($image['title'])) {
69
        $caption = $image['title'];
70
      }
71
      elseif (!empty($image['alt'])) {
72
        $caption = $image['alt'];
73
      }
74
      elseif (!empty($entity_title)) {
75
        $caption = $entity_title;
76
      }
77
      else {
78
        $caption = '';
79
      }
80
      break;
81
    case 'title':
82
      $caption = $image['title'];
83
      break;
84
    case 'alt':
85
      $caption = $image['alt'];
86
      break;
87
    case 'node_title':
88
      $caption = $entity_title;
89
      break;
90
    case 'custom':
91
      $caption = token_replace($settings['colorbox_caption_custom'], array($entity_type => $entity, 'file' => (object) $item), array('clear' => TRUE));
92
      break;
93
    default:
94
      $caption = '';
95
  }
96

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

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

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

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

    
144
  return theme('colorbox_imagefield', array('image' => $image, 'path' => $path, 'title' => $caption, 'gid' => $gallery_id));
145
}
146

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

    
162
  if ($variables['image']['style_name'] == 'hide') {
163
    $image = '';
164
    $class[] = 'js-hide';
165
  }
166
  elseif (!empty($variables['image']['style_name'])) {
167
    $image = theme('image_style', $variables['image']);
168
  }
169
  else {
170
    $image = theme('image', $variables['image']);
171
  }
172

    
173
  $options = drupal_parse_url($variables['path']);
174
  $options += array(
175
    'html' => TRUE,
176
    'attributes' => array(
177
      'title' => $variables['title'],
178
      'class' => $class,
179
      'rel' => $variables['gid'],
180
    ),
181
    'language' => array('language' => NULL),
182
  );
183

    
184
  return l($image, $options['path'], $options);
185
}
186

    
187
/**
188
 * Preprocess variables for the colorbox-insert-image.tpl.php file.
189
 */
190
function template_preprocess_colorbox_insert_image(&$variables) {
191
  $item = $variables['item'];
192
  $variables['file'] = file_load($item['fid']);
193
  $variables['style_name'] = $item['style_name'];
194
  $variables['width'] = isset($item['width']) ? $item['width'] : NULL;
195
  $variables['height'] = isset($item['height']) ? $item['height'] : NULL;
196

    
197
  // Determine dimensions of the image after the image style transformations.
198
  image_style_transform_dimensions($variables['style_name'], $variables);
199

    
200
  $class = array();
201
  if (!empty($variables['widget']['settings']['insert_class'])) {
202
    $class = explode(' ', $variables['widget']['settings']['insert_class']);
203
  }
204
  $class[] = 'image-' . $variables['style_name'];
205

    
206
  foreach ($class as $key => $value) {
207
    $class[$key] = drupal_html_class($value);
208
  }
209

    
210
  $variables['class'] = implode(' ', $class);
211

    
212
  $variables['uri'] = image_style_path($variables['style_name'], $variables['file']->uri);
213
  $absolute = isset($variables['widget']['settings']['insert_absolute']) ? $variables['widget']['settings']['insert_absolute'] : NULL;
214
  $variables['url'] = insert_create_url($variables['uri'], $absolute, variable_get('clean_url'));
215

    
216
  // http://drupal.org/node/1923336
217
  if (function_exists('image_style_path_token')) {
218
    $token_query = array(IMAGE_DERIVATIVE_TOKEN => image_style_path_token($variables['style_name'], $variables['file']->uri));
219
    $variables['url'] .= (strpos($variables['url'], '?') !== FALSE ? '&' : '?') . drupal_http_build_query($token_query);
220
  }
221

    
222
  if ($style_name = variable_get('colorbox_image_style', '')) {
223
    $variables['path'] = image_style_url($style_name, $variables['file']->uri);
224
  }
225
  else {
226
    $variables['path'] = file_create_url($variables['file']->uri);
227
  }
228

    
229
  $variables['gallery_id'] = '';
230
  switch (variable_get('colorbox_insert_gallery', 0)) {
231
    case 0:
232
    case 1:
233
    case 2:
234
      $variables['gallery_id'] = 'gallery-all';
235
      break;
236
    case 3:
237
      $variables['gallery_id'] = '';
238
      break;
239
  }
240
}