Projet

Général

Profil

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

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

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
  $item = $variables['item'];
21
  $entity_type = $variables['entity_type'];
22
  $entity = $variables['entity'];
23
  $field = $variables['field'];
24
  $settings = $variables['display_settings'];
25

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

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

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

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

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

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

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

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

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

    
126
  if ($style_name = $settings['colorbox_image_style']) {
127
    $path = image_style_url($style_name, $image['path']);
128
  }
129
  else {
130
    $path = file_create_url($image['path']);
131
  }
132

    
133
  return theme('colorbox_imagefield', array('image' => $image, 'path' => $path, 'title' => $caption, 'gid' => $gallery_id));
134
}
135

    
136
/**
137
 * Returns HTML for an image using a specific Colorbox image style.
138
 *
139
 * @param $variables
140
 *   An associative array containing:
141
 *   - image: image item as array.
142
 *   - path: The path of the image that should be displayed in the Colorbox.
143
 *   - title: The title text that will be used as a caption in the Colorbox.
144
 *   - gid: Gallery id for Colorbox image grouping.
145
 *
146
 * @ingroup themeable
147
 */
148
function theme_colorbox_imagefield($variables) {
149
  $class = array('colorbox');
150

    
151
  if ($variables['image']['style_name'] == 'hide') {
152
    $image = '';
153
    $class[] = 'js-hide';
154
  }
155
  elseif (!empty($variables['image']['style_name'])) {
156
    $image = theme('image_style', $variables['image']);
157
  }
158
  else {
159
    $image = theme('image', $variables['image']);
160
  }
161

    
162
  $options = drupal_parse_url($variables['path']);
163
  $options += array(
164
    'html' => TRUE,
165
    'attributes' => array(
166
      'title' => $variables['title'],
167
      'class' => $class,
168
      'rel' => $variables['gid'],
169
    ),
170
    'language' => array('language' => NULL),
171
  );
172

    
173
  return l($image, $options['path'], $options);
174
}
175

    
176
/**
177
 * Preprocess variables for the colorbox-insert-image.tpl.php file.
178
 */
179
function template_preprocess_colorbox_insert_image(&$variables) {
180
  $item = $variables['item'];
181
  $variables['file'] = file_load($item['fid']);
182
  $variables['style_name'] = $item['style_name'];
183
  $variables['width'] = isset($item['width']) ? $item['width'] : NULL;
184
  $variables['height'] = isset($item['height']) ? $item['height'] : NULL;
185

    
186
  // Determine dimensions of the image after the image style transformations.
187
  image_style_transform_dimensions($variables['style_name'], $variables);
188

    
189
  $class = array();
190
  if (!empty($variables['widget']['settings']['insert_class'])) {
191
    $class = explode(' ', $variables['widget']['settings']['insert_class']);
192
  }
193
  $class[] = 'image-' . $variables['style_name'];
194

    
195
  foreach ($class as $key => $value) {
196
    $class[$key] = drupal_html_class($value);
197
  }
198

    
199
  $variables['class'] = implode(' ', $class);
200

    
201
  $variables['uri'] = image_style_path($variables['style_name'], $variables['file']->uri);
202
  $absolute = isset($variables['widget']['settings']['insert_absolute']) ? $variables['widget']['settings']['insert_absolute'] : NULL;
203
  $variables['url'] = insert_create_url($variables['uri'], $absolute, variable_get('clean_url'));
204

    
205
  // http://drupal.org/node/1923336
206
  if (function_exists('image_style_path_token')) {
207
    $token_query = array(IMAGE_DERIVATIVE_TOKEN => image_style_path_token($variables['style_name'], $variables['file']->uri));
208
    $variables['url'] .= (strpos($variables['url'], '?') !== FALSE ? '&' : '?') . drupal_http_build_query($token_query);
209
  }
210

    
211
  if ($style_name = variable_get('colorbox_image_style', '')) {
212
    $variables['path'] = image_style_url($style_name, $variables['file']->uri);
213
  }
214
  else {
215
    $variables['path'] = file_create_url($variables['file']->uri);
216
  }
217

    
218
  $variables['gallery_id'] = '';
219
  switch (variable_get('colorbox_insert_gallery', 0)) {
220
    case 0:
221
    case 1:
222
    case 2:
223
      $variables['gallery_id'] = 'gallery-all';
224
      break;
225
    case 3:
226
      $variables['gallery_id'] = '';
227
      break;
228
  }
229
}