-
Notifications
You must be signed in to change notification settings - Fork 7
/
colorbox.theme.inc
264 lines (227 loc) · 7.95 KB
/
colorbox.theme.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
/**
* @file
* Colorbox theme functions.
*/
/**
* Returns HTML for a Colorbox image field formatter.
*
* @param array $variables
* An associative array containing:
* - item: An array of image data.
* - image_style: An optional image style.
* - path: An array containing the link 'path' and link 'options'.
*
* @return string
* An HTML string representing the themed output.
*
* @ingroup themeable
*/
function theme_colorbox_image_formatter($variables) {
global $language;
static $gallery_token = NULL;
$item = $variables['item'];
$entity_type = $variables['entity_type'];
$entity = $variables['entity'];
$field = $variables['field'];
$settings = $variables['display_settings'];
$image = array(
'uri' => $item['uri'],
'alt' => isset($item['alt']) ? $item['alt'] : '',
'title' => isset($item['title']) ? $item['title'] : '',
'style_name' => $settings['colorbox_node_style'],
);
if ($variables['delta'] == 0 && !empty($settings['colorbox_node_style_first'])) {
$image['style_name'] = $settings['colorbox_node_style_first'];
}
if (isset($item['width']) && isset($item['height'])) {
$image['width'] = $item['width'];
$image['height'] = $item['height'];
}
if (isset($item['attributes'])) {
$image['attributes'] = $item['attributes'];
}
// Allow image attributes to be overridden.
if (isset($variables['item']['override']['attributes'])) {
foreach (array('width', 'height', 'alt', 'title') as $key) {
if (isset($variables['item']['override']['attributes'][$key])) {
$image[$key] = $variables['item']['override']['attributes'][$key];
unset($variables['item']['override']['attributes'][$key]);
}
}
if (isset($image['attributes'])) {
$image['attributes'] = $variables['item']['override']['attributes'] + $image['attributes'];
}
else {
$image['attributes'] = $variables['item']['override']['attributes'];
}
}
$entity_title = entity_label($entity_type, $entity);
switch ($settings['colorbox_caption']) {
case 'auto':
// If the title is empty use alt or the entity title in that order.
if (!empty($image['title'])) {
$caption = $image['title'];
}
elseif (!empty($image['alt'])) {
$caption = $image['alt'];
}
elseif (!empty($entity_title)) {
$caption = $entity_title;
}
else {
$caption = '';
}
break;
case 'title':
$caption = $image['title'];
break;
case 'alt':
$caption = $image['alt'];
break;
case 'node_title':
$caption = $entity_title;
break;
case 'custom':
$caption = token_replace($settings['colorbox_caption_custom'], array($entity_type => $entity, 'file' => (object) $item), array('clear' => TRUE, 'language' => $language));
break;
default:
$caption = '';
}
// Shorten the caption for the example styles or when caption shortening is
// active.
$colorbox_style = config_get('colorbox.settings', 'colorbox_style');
$trim_length = config_get('colorbox.settings', 'colorbox_caption_trim_length');
if (((strpos($colorbox_style, 'colorbox/example') !== FALSE) || config_get('colorbox.settings', 'colorbox_caption_trim')) && (backdrop_strlen($caption) > $trim_length)) {
$caption = backdrop_substr($caption, 0, $trim_length - 5) . '...';
}
// Build the gallery id.
list($id) = entity_extract_ids($entity_type, $entity);
$entity_id = !empty($id) ? $entity_type . '-' . $id : 'entity-id';
switch ($settings['colorbox_gallery']) {
case 'post':
$gallery_id = 'gallery-' . $entity_id;
break;
case 'page':
$gallery_id = 'gallery-all';
break;
case 'field_post':
$gallery_id = 'gallery-' . $entity_id . '-' . $field['field_name'];
break;
case 'field_page':
$gallery_id = isset($field['field_name']) ? 'gallery-' . $field['field_name'] : 'gallery-page';
break;
case 'custom':
$gallery_id = $settings['colorbox_gallery_custom'];
break;
default:
$gallery_id = '';
}
// If gallery id is not empty add unique per-request token to avoid images
// being added manually to galleries.
$unique_token = config_get('colorbox.settings', 'colorbox_unique_token');
if (!empty($gallery_id) && $unique_token) {
// Check if gallery token has already been set, we need to reuse the token
// for the whole request.
if (is_null($gallery_token)) {
// We use a short token since randomness is not critical.
$gallery_token = backdrop_random_key(8);
}
$gallery_id = $gallery_id . '-' . $gallery_token;
}
if ($style_name = $settings['colorbox_image_style']) {
$path = image_style_url($style_name, $image['uri']);
}
else {
$path = file_create_url($image['uri']);
}
return theme('colorbox_imagefield', array(
'image' => $image,
'path' => $path,
'title' => $caption,
'gid' => $gallery_id,
));
}
/**
* Returns HTML for an image using a specific Colorbox image style.
*
* @param array $variables
* An associative array containing:
* - image: image item as array.
* - path: The path of the image that should be displayed in the Colorbox.
* - title: The title text that will be used as a caption in the Colorbox.
* - gid: Gallery id for Colorbox image grouping.
*
* @return string
* An HTML string containing a link to the given path.
*
* @ingroup themeable
*/
function theme_colorbox_imagefield($variables) {
$class = array('colorbox');
if ($variables['image']['style_name'] == 'hide') {
$image = '';
$class[] = 'js-hide';
}
elseif (!empty($variables['image']['style_name'])) {
$image = theme('image_style', $variables['image']);
}
else {
$image = theme('image', $variables['image']);
}
$options = backdrop_parse_url($variables['path']);
$options += array(
'html' => TRUE,
'attributes' => array(
'title' => $variables['title'],
'class' => $class,
'data-colorbox-gallery' => $variables['gid'],
'data-cbox-img-attrs' => '{"title": "' . $variables['image']['title'] . '", "alt": "' . $variables['image']['alt'] . '"}',
),
);
return l($image, $options['path'], $options);
}
/**
* Preprocess variables for the colorbox-insert-image.tpl.php file.
*
* @param array $variables
* Associative array of variables for that template.
*/
function template_preprocess_colorbox_insert_image(&$variables) {
$item = $variables['item'];
$variables['file'] = file_load($item['fid']);
$variables['style_name'] = $item['style_name'];
$variables['width'] = isset($item['width']) ? $item['width'] : NULL;
$variables['height'] = isset($item['height']) ? $item['height'] : NULL;
// Determine dimensions of the image after the image style transformations.
image_style_transform_dimensions($variables['style_name'], $variables);
$class = array();
if (!empty($variables['widget']['settings']['insert_class'])) {
$class = explode(' ', $variables['widget']['settings']['insert_class']);
}
$class[] = 'image-' . $variables['style_name'];
foreach ($class as $key => $value) {
$class[$key] = backdrop_html_class($value);
}
$variables['class'] = implode(' ', $class);
$variables['uri'] = image_style_path($variables['style_name'], $variables['file']->uri);
$absolute = isset($variables['widget']['settings']['insert_absolute']) ? $variables['widget']['settings']['insert_absolute'] : NULL;
$variables['url'] = insert_create_url($variables['uri'], $absolute, config_get('system.core', 'clean_url'));
if ($style_name = config_get('colorbox.settings', 'colorbox_image_style')) {
$variables['path'] = image_style_url($style_name, $variables['file']->uri);
}
else {
$variables['path'] = file_create_url($variables['file']->uri);
}
$variables['gallery_id'] = '';
switch (config_get('colorbox.settings', 'colorbox_insert_gallery')) {
case 0:
case 1:
case 2:
$variables['gallery_id'] = 'gallery-all';
break;
case 3:
$variables['gallery_id'] = '';
break;
}
}