Projet

Général

Profil

Paste
Télécharger (6,55 ko) Statistiques
| Branche: | Révision:

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

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
  $entity_title = entity_label($entity_type, $entity);
47

    
48
  switch ($settings['colorbox_caption']) {
49
     case 'auto':
50
      // If the title is empty use alt or the entity title in that order.
51
      if (!empty($image['title'])) {
52
        $caption = $image['title'];
53
      }
54
      elseif (!empty($image['alt'])) {
55
        $caption = $image['alt'];
56
      }
57
      elseif (!empty($entity_title)) {
58
        $caption = $entity_title;
59
      }
60
      else {
61
        $caption = '';
62
      }
63
      break;
64
    case 'title':
65
      $caption = $image['title'];
66
      break;
67
    case 'alt':
68
      $caption = $image['alt'];
69
      break;
70
    case 'node_title':
71
      $caption = $entity_title;
72
      break;
73
    case 'custom':
74
      $caption = token_replace($settings['colorbox_caption_custom'], array($entity_type => $entity, 'file' => (object) $item), array('clear' => TRUE));
75
      break;
76
    default:
77
      $caption = '';
78
  }
79

    
80
  // Shorten the caption for the example styles or when caption shortening is active.
81
  $colorbox_style = variable_get('colorbox_style', 'default');
82
  $trim_length = variable_get('colorbox_caption_trim_length', 75);
83
  if (((strpos($colorbox_style, 'colorbox/example') !== FALSE) || variable_get('colorbox_caption_trim', 0)) && (drupal_strlen($caption) > $trim_length)) {
84
    $caption = drupal_substr($caption, 0, $trim_length - 5) . '...';
85
  }
86

    
87
  // Build the gallery id.
88
  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
89
  $entity_id = !empty($id) ? $entity_type . '-' . $id : 'entity-id';
90
  switch ($settings['colorbox_gallery']) {
91
    case 'post':
92
      $gallery_id = 'gallery-' . $entity_id;
93
      break;
94
    case 'page':
95
      $gallery_id = 'gallery-all';
96
      break;
97
    case 'field_post':
98
      $gallery_id = 'gallery-' . $entity_id . '-' . $field['field_name'];
99
      break;
100
    case 'field_page':
101
      $gallery_id = 'gallery-' . $field['field_name'];
102
      break;
103
    case 'custom':
104
      $gallery_id = $settings['colorbox_gallery_custom'];
105
      break;
106
    default:
107
      $gallery_id = '';
108
  }
109

    
110
  if ($style_name = $settings['colorbox_image_style']) {
111
    $path = image_style_url($style_name, $image['path']);
112
  }
113
  else {
114
    $path = file_create_url($image['path']);
115
  }
116

    
117
  return theme('colorbox_imagefield', array('image' => $image, 'path' => $path, 'title' => $caption, 'gid' => $gallery_id));
118
}
119

    
120
/**
121
 * Returns HTML for an image using a specific Colorbox image style.
122
 *
123
 * @param $variables
124
 *   An associative array containing:
125
 *   - image: image item as array.
126
 *   - path: The path of the image that should be displayed in the Colorbox.
127
 *   - title: The title text that will be used as a caption in the Colorbox.
128
 *   - gid: Gallery id for Colorbox image grouping.
129
 *
130
 * @ingroup themeable
131
 */
132
function theme_colorbox_imagefield($variables) {
133
  $class = array('colorbox');
134

    
135
  if ($variables['image']['style_name'] == 'hide') {
136
    $image = '';
137
    $class[] = 'js-hide';
138
  }
139
  elseif (!empty($variables['image']['style_name'])) {
140
    $image = theme('image_style', $variables['image']);
141
  }
142
  else {
143
    $image = theme('image', $variables['image']);
144
  }
145

    
146
  $options = drupal_parse_url($variables['path']);
147
  $options += array(
148
    'html' => TRUE,
149
    'attributes' => array(
150
      'title' => $variables['title'],
151
      'class' => $class,
152
      'rel' => $variables['gid'],
153
    ),
154
    'language' => array('language' => NULL),
155
  );
156

    
157
  return l($image, $options['path'], $options);
158
}
159

    
160
/**
161
 * Preprocess variables for the colorbox-insert-image.tpl.php file.
162
 */
163
function template_preprocess_colorbox_insert_image(&$variables) {
164
  $item = $variables['item'];
165
  $variables['file'] = file_load($item['fid']);
166
  $variables['style_name'] = $item['style_name'];
167
  $variables['width'] = isset($item['width']) ? $item['width'] : NULL;
168
  $variables['height'] = isset($item['height']) ? $item['height'] : NULL;
169

    
170
  // Determine dimensions of the image after the image style transformations.
171
  image_style_transform_dimensions($variables['style_name'], $variables);
172

    
173
  $class = array();
174
  if (!empty($variables['widget']['settings']['insert_class'])) {
175
    $class = explode(' ', $variables['widget']['settings']['insert_class']);
176
  }
177
  $class[] = 'image-' . $variables['style_name'];
178

    
179
  foreach ($class as $key => $value) {
180
    $class[$key] = drupal_html_class($value);
181
  }
182

    
183
  $variables['class'] = implode(' ', $class);
184

    
185
  $variables['uri'] = image_style_path($variables['style_name'], $variables['file']->uri);
186
  $absolute = isset($variables['widget']['settings']['insert_absolute']) ? $variables['widget']['settings']['insert_absolute'] : NULL;
187
  $variables['url'] = insert_create_url($variables['uri'], $absolute, variable_get('clean_url'));
188

    
189
  // http://drupal.org/node/1923336
190
  if (function_exists('image_style_path_token')) {
191
    $token_query = array(IMAGE_DERIVATIVE_TOKEN => image_style_path_token($variables['style_name'], $variables['file']->uri));
192
    $variables['url'] .= (strpos($variables['url'], '?') !== FALSE ? '&' : '?') . drupal_http_build_query($token_query);
193
  }
194

    
195
  if ($style_name = variable_get('colorbox_image_style', '')) {
196
    $variables['path'] = image_style_url($style_name, $variables['file']->uri);
197
  }
198
  else {
199
    $variables['path'] = file_create_url($variables['file']->uri);
200
  }
201

    
202
  $variables['gallery_id'] = '';
203
  switch (variable_get('colorbox_insert_gallery', 0)) {
204
    case 0:
205
    case 1:
206
    case 2:
207
      $variables['gallery_id'] = 'gallery-all';
208
      break;
209
    case 3:
210
      $variables['gallery_id'] = '';
211
      break;
212
  }
213
}