1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Views handlers for Colorbox module.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* A handler to provide a field that is completely custom by the administrator.
|
10
|
*
|
11
|
* @ingroup views_field_handlers
|
12
|
* @codingStandardsIgnoreStart
|
13
|
*/
|
14
|
class colorbox_handler_field_colorbox extends views_handler_field {
|
15
|
|
16
|
/**
|
17
|
* Query.
|
18
|
*
|
19
|
* @codingStandardsIgnoreEnd
|
20
|
*/
|
21
|
public function query() {
|
22
|
// Do nothing, as this handler does not need to do anything to the query
|
23
|
// itself.
|
24
|
}
|
25
|
|
26
|
/**
|
27
|
* Option definition.
|
28
|
*
|
29
|
* @codingStandardsIgnoreStart
|
30
|
*/
|
31
|
public function option_definition() {
|
32
|
// @codingStandardsIgnoreEnd
|
33
|
$options = parent::option_definition();
|
34
|
|
35
|
$options['trigger_field'] = array('default' => '');
|
36
|
$options['popup'] = array('default' => '');
|
37
|
$options['caption'] = array('default' => '');
|
38
|
$options['gid'] = array('default' => TRUE);
|
39
|
$options['custom_gid'] = array('default' => '');
|
40
|
$options['width'] = array('default' => '600px');
|
41
|
$options['height'] = array('default' => '400px');
|
42
|
|
43
|
return $options;
|
44
|
}
|
45
|
|
46
|
/**
|
47
|
* Options for form.
|
48
|
*
|
49
|
* @codingStandardsIgnoreStart
|
50
|
*/
|
51
|
public function options_form(&$form, &$form_state) {
|
52
|
// @codingStandardsIgnoreEnd
|
53
|
parent::options_form($form, $form_state);
|
54
|
|
55
|
// Get a list of the available fields and arguments for trigger field and
|
56
|
// token replacement.
|
57
|
$options = array();
|
58
|
$fields = array('trigger_field' => t('- None -'));
|
59
|
foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
|
60
|
$options[t('Fields')]["[$field]"] = $handler->ui_name();
|
61
|
// We only use fields up to (and including) this one.
|
62
|
if ($field == $this->options['id']) {
|
63
|
break;
|
64
|
}
|
65
|
|
66
|
$fields[$field] = $handler->definition['title'];
|
67
|
}
|
68
|
// This lets us prepare the key as we want it printed.
|
69
|
$count = 0;
|
70
|
foreach ($this->view->display_handler->get_handlers('argument') as $handler) {
|
71
|
$options[t('Arguments')]['%' . ++$count] = t('@argument title', array('@argument' => $handler->ui_name()));
|
72
|
$options[t('Arguments')]['!' . $count] = t('@argument input', array('@argument' => $handler->ui_name()));
|
73
|
}
|
74
|
|
75
|
$this->document_self_tokens($options[t('Fields')]);
|
76
|
|
77
|
// Default text.
|
78
|
$patterns = t('<p>You must add some additional fields to this display before using this field. These fields may be marked as <em>Exclude from display</em> if you prefer. Note that due to rendering order, you cannot use fields that come after this field; if you need a field not listed here, rearrange your fields.</p>');
|
79
|
// We have some options, so make a list.
|
80
|
if (!empty($options)) {
|
81
|
$patterns = t('<p>The following tokens are available for this field. Note that due to rendering order, you cannot use fields that come after this field; if you need a field not listed here, rearrange your fields.
|
82
|
If you would like to have the characters %5B and %5D please use the html entity codes \'%5B\' or \'%5D\' or they will get replaced with empty space.</p>');
|
83
|
foreach (array_keys($options) as $type) {
|
84
|
if (!empty($options[$type])) {
|
85
|
$items = array();
|
86
|
foreach ($options[$type] as $key => $value) {
|
87
|
$items[] = $key . ' == ' . $value;
|
88
|
}
|
89
|
$patterns .= theme('item_list',
|
90
|
array(
|
91
|
'items' => $items,
|
92
|
'type' => $type,
|
93
|
));
|
94
|
}
|
95
|
}
|
96
|
}
|
97
|
|
98
|
$form['trigger_field'] = array(
|
99
|
'#type' => 'select',
|
100
|
'#title' => t('Trigger field'),
|
101
|
'#description' => t('Select the field that should be turned into the trigger for the Colorbox. Only fields that appear before this one in the field list may be used.'),
|
102
|
'#options' => $fields,
|
103
|
'#default_value' => $this->options['trigger_field'],
|
104
|
'#weight' => -12,
|
105
|
);
|
106
|
|
107
|
$form['popup'] = array(
|
108
|
'#type' => 'textarea',
|
109
|
'#title' => t('Popup'),
|
110
|
'#description' => t('The Colorbox popup content. You may include HTML. You may enter data from this view as per the "Replacement patterns" below.'),
|
111
|
'#default_value' => $this->options['popup'],
|
112
|
'#weight' => -11,
|
113
|
);
|
114
|
|
115
|
$form['caption'] = array(
|
116
|
'#type' => 'textfield',
|
117
|
'#title' => t('Caption'),
|
118
|
'#description' => t('The Colorbox Caption. You may include HTML. You may enter data from this view as per the "Replacement patterns" below.'),
|
119
|
'#default_value' => $this->options['caption'],
|
120
|
'#weight' => -10,
|
121
|
);
|
122
|
|
123
|
$form['gid'] = array(
|
124
|
'#type' => 'checkbox',
|
125
|
'#title' => t('Automatic generated Colorbox gallery'),
|
126
|
'#description' => t('Enable Colorbox gallery using a generated gallery id for this view.'),
|
127
|
'#default_value' => $this->options['gid'],
|
128
|
'#weight' => -9,
|
129
|
);
|
130
|
|
131
|
$form['custom_gid'] = array(
|
132
|
'#type' => 'textfield',
|
133
|
'#title' => t('Custom Colorbox gallery'),
|
134
|
'#description' => t('Enable Colorbox gallery with a given string as gallery. Overrides the automatically generated gallery id above. You may enter data from this view as per the "Replacement patterns" below.'),
|
135
|
'#default_value' => $this->options['custom_gid'],
|
136
|
'#weight' => -8,
|
137
|
);
|
138
|
|
139
|
$form['width'] = array(
|
140
|
'#type' => 'textfield',
|
141
|
'#title' => t('Width'),
|
142
|
'#description' => t('Specify the width of the Colorbox popup window. Because the content is dynamic, we cannot detect this value automatically. Example: "100%", 500, "500px".'),
|
143
|
'#default_value' => $this->options['width'],
|
144
|
'#weight' => -6,
|
145
|
);
|
146
|
|
147
|
$form['height'] = array(
|
148
|
'#type' => 'textfield',
|
149
|
'#title' => t('Height'),
|
150
|
'#description' => t('Specify the height of the Colorbox popup window. Because the content is dynamic, we cannot detect this value automatically. Example: "100%", 500, "500px".'),
|
151
|
'#default_value' => $this->options['height'],
|
152
|
'#weight' => -7,
|
153
|
);
|
154
|
|
155
|
$form['patterns'] = array(
|
156
|
'#type' => 'fieldset',
|
157
|
'#title' => t('Replacement patterns'),
|
158
|
'#collapsible' => TRUE,
|
159
|
'#collapsed' => TRUE,
|
160
|
'#value' => $patterns,
|
161
|
);
|
162
|
}
|
163
|
|
164
|
/**
|
165
|
* Render the trigger field and its linked popup information.
|
166
|
*/
|
167
|
public function render($values) {
|
168
|
// Load the necessary js file for Colorbox activation.
|
169
|
if (_colorbox_active() && !variable_get('colorbox_inline', 0)) {
|
170
|
drupal_add_js(drupal_get_path('module', 'colorbox') . '/js/colorbox_inline.js');
|
171
|
}
|
172
|
|
173
|
// We need to have multiple unique IDs, one for each record.
|
174
|
static $i = 0;
|
175
|
$i = mt_rand();
|
176
|
|
177
|
// Return nothing if no trigger filed is selected.
|
178
|
if (empty($this->options['trigger_field'])) {
|
179
|
return;
|
180
|
}
|
181
|
|
182
|
// Get the token information and generate the value for the popup and the
|
183
|
// caption.
|
184
|
$tokens = $this->get_render_tokens($this->options['alter']);
|
185
|
$popup = filter_xss_admin($this->options['popup']);
|
186
|
$caption = filter_xss_admin($this->options['caption']);
|
187
|
$gallery = filter_xss_admin($this->options['custom_gid']);
|
188
|
$popup = strtr($popup, $tokens);
|
189
|
$caption = strtr($caption, $tokens);
|
190
|
$gallery = drupal_html_class(strtr($gallery, $tokens));
|
191
|
|
192
|
// Return nothing if popup is empty.
|
193
|
if (empty($popup)) {
|
194
|
return;
|
195
|
}
|
196
|
|
197
|
$width = $this->options['width'] ? $this->options['width'] : '';
|
198
|
$height = $this->options['height'] ? $this->options['height'] : '';
|
199
|
$gallery_id = !empty($gallery) ? $gallery : ($this->options['gid'] ? 'gallery-' . $this->view->name : '');
|
200
|
$link_text = $tokens["[{$this->options['trigger_field']}]"];
|
201
|
$link_options = array(
|
202
|
'html' => TRUE,
|
203
|
'fragment' => 'colorbox-inline-' . $i,
|
204
|
'query' => array(
|
205
|
'width' => $width,
|
206
|
'height' => $height,
|
207
|
'title' => $caption,
|
208
|
'inline' => 'true',
|
209
|
),
|
210
|
'attributes' => array(
|
211
|
'class' => array('colorbox-inline'),
|
212
|
'rel' => $gallery_id,
|
213
|
),
|
214
|
);
|
215
|
// Remove any parameters that aren't set.
|
216
|
$link_options['query'] = array_filter($link_options['query']);
|
217
|
|
218
|
// If the nid is present make the link degrade to the node page if
|
219
|
// JavaScript is off.
|
220
|
$link_target = isset($values->nid) ? 'node/' . $values->nid : '';
|
221
|
$link_tag = l($link_text, $link_target, $link_options);
|
222
|
|
223
|
// The outside div is there to hide all of the divs because if the specific
|
224
|
// Colorbox div is hidden it won't show up as a Colorbox.
|
225
|
return $link_tag . '<div style="display: none;"><div id="colorbox-inline-' . $i . '">' . $popup . '</div></div>';
|
226
|
}
|
227
|
|
228
|
}
|