Projet

Général

Profil

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

root / drupal7 / sites / all / modules / colorbox / colorbox.theme.inc @ 74f6bef0

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 (isset($item['width']) && isset($item['height'])) {
34
    $image['width'] = $item['width'];
35
    $image['height'] = $item['height'];
36
  }
37

    
38
  $entity_title = entity_label($entity_type, $entity);
39

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

    
72
  // Shorten the caption for the example styles or when caption shortening is active.
73
  $colorbox_style = variable_get('colorbox_style', 'default');
74
  $trim_length = variable_get('colorbox_caption_trim_length', 75);
75
  if (((strpos($colorbox_style, 'colorbox/example') !== FALSE) || variable_get('colorbox_caption_trim', 0)) && (drupal_strlen($caption) > $trim_length)) {
76
    $caption = drupal_substr($caption, 0, $trim_length - 5) . '...';
77
  }
78

    
79
  // Build the gallery id.
80
  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
81
  $entity_id = !empty($id) ? $entity_type . '-' . $id : 'entity-id';
82
  switch ($settings['colorbox_gallery']) {
83
    case 'post':
84
      $gallery_id = 'gallery-' . $entity_id;
85
      break;
86
    case 'page':
87
      $gallery_id = 'gallery-all';
88
      break;
89
    case 'field_post':
90
      $gallery_id = 'gallery-' . $entity_id . '-' . $field['field_name'];
91
      break;
92
    case 'field_page':
93
      $gallery_id = 'gallery-' . $field['field_name'];
94
      break;
95
    case 'custom':
96
      $gallery_id = $settings['colorbox_gallery_custom'];
97
      break;
98
    default:
99
      $gallery_id = '';
100
  }
101

    
102
  if ($style_name = $settings['colorbox_image_style']) {
103
    $path = image_style_url($style_name, $image['path']);
104
  }
105
  else {
106
    $path = file_create_url($image['path']);
107
  }
108

    
109
  return theme('colorbox_imagefield', array('image' => $image, 'path' => $path, 'title' => $caption, 'gid' => $gallery_id));
110
}
111

    
112
/**
113
 * Returns HTML for an image using a specific Colorbox image style.
114
 *
115
 * @param $variables
116
 *   An associative array containing:
117
 *   - image: image item as array.
118
 *   - path: The path of the image that should be displayed in the Colorbox.
119
 *   - title: The title text that will be used as a caption in the Colorbox.
120
 *   - gid: Gallery id for Colorbox image grouping.
121
 *
122
 * @ingroup themeable
123
 */
124
function theme_colorbox_imagefield($variables) {
125
  $class = array('colorbox');
126

    
127
  if ($variables['image']['style_name'] == 'hide') {
128
    $image = '';
129
    $class[] = 'js-hide';
130
  }
131
  elseif (!empty($variables['image']['style_name'])) {
132
    $image = theme('image_style', $variables['image']);
133
  }
134
  else {
135
    $image = theme('image', $variables['image']);
136
  }
137

    
138
  $options = drupal_parse_url($variables['path']);
139
  $options += array(
140
    'html' => TRUE,
141
    'attributes' => array(
142
      'title' => $variables['title'],
143
      'class' => $class,
144
      'rel' => $variables['gid'],
145
    ),
146
    'language' => array('language' => NULL),
147
  );
148

    
149
  return l($image, $options['path'], $options);
150
}
151

    
152
/**
153
 * Preprocess variables for the colorbox-insert-image.tpl.php file.
154
 */
155
function template_preprocess_colorbox_insert_image(&$variables) {
156
  $variables['file'] = file_load($variables['item']['fid']);
157
  $variables['style_name'] = $variables['item']['style_name'];
158
  $variables['width'] = $variables['item']['width'];
159
  $variables['height'] = $variables['item']['height'];
160

    
161
  // Determine dimensions of the image after the image style transformations.
162
  image_style_transform_dimensions($variables['style_name'], $variables);
163

    
164
  $class = array();
165
  if (!empty($variables['widget']['settings']['insert_class'])) {
166
    $class = explode(' ', $variables['widget']['settings']['insert_class']);
167
  }
168
  $class[] = 'image-' . $variables['style_name'];
169

    
170
  foreach ($class as $key => $value) {
171
    $class[$key] = drupal_html_class($value);
172
  }
173

    
174
  $variables['class'] = implode(' ', $class);
175

    
176
  $variables['uri'] = image_style_path($variables['style_name'], $variables['file']->uri);
177
  $absolute = isset($variables['widget']['settings']['insert_absolute']) ? $variables['widget']['settings']['insert_absolute'] : NULL;
178
  $variables['url'] = insert_create_url($variables['uri'], $absolute, variable_get('clean_url'));
179

    
180
  // http://drupal.org/node/1923336
181
  if (function_exists('image_style_path_token')) {
182
    $token_query = array(IMAGE_DERIVATIVE_TOKEN => image_style_path_token($variables['style_name'], $variables['file']->uri));
183
    $variables['url'] .= (strpos($variables['url'], '?') !== FALSE ? '&' : '?') . drupal_http_build_query($token_query);
184
  }
185

    
186
  if ($style_name = variable_get('colorbox_image_style', '')) {
187
    $variables['path'] = image_style_url($style_name, $variables['file']->uri);
188
  }
189
  else {
190
    $variables['path'] = file_create_url($variables['file']->uri);
191
  }
192

    
193
  $variables['gallery_id'] = '';
194
  switch (variable_get('colorbox_insert_gallery', 0)) {
195
    case 0:
196
    case 1:
197
    case 2:
198
      $variables['gallery_id'] = 'gallery-all';
199
      break;
200
    case 3:
201
      $variables['gallery_id'] = '';
202
      break;
203
  }
204
}