1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Definition of views_handler_filter_term_node_tid.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Filter by term id.
|
10
|
*
|
11
|
* @ingroup views_filter_handlers
|
12
|
*/
|
13
|
class views_handler_filter_term_node_tid extends views_handler_filter_many_to_one {
|
14
|
|
15
|
/**
|
16
|
* Stores the exposed input for this filter.
|
17
|
*/
|
18
|
public $validated_exposed_input = NULL;
|
19
|
|
20
|
/**
|
21
|
* {@inheritdoc}
|
22
|
*/
|
23
|
public function init(&$view, &$options) {
|
24
|
parent::init($view, $options);
|
25
|
if (!empty($this->definition['vocabulary'])) {
|
26
|
$this->options['vocabulary'] = $this->definition['vocabulary'];
|
27
|
}
|
28
|
|
29
|
// Convert legacy vid option to machine name vocabulary.
|
30
|
if (isset($this->options['vid']) && !empty($this->options['vid']) & empty($this->options['vocabulary'])) {
|
31
|
$vocabularies = taxonomy_get_vocabularies();
|
32
|
$vid = $this->options['vid'];
|
33
|
if (isset($vocabularies[$vid], $vocabularies[$vid]->machine_name)) {
|
34
|
$this->options['vocabulary'] = $vocabularies[$vid]->machine_name;
|
35
|
}
|
36
|
}
|
37
|
}
|
38
|
|
39
|
/**
|
40
|
* {@inheritdoc}
|
41
|
*/
|
42
|
public function has_extra_options() {
|
43
|
return TRUE;
|
44
|
}
|
45
|
|
46
|
/**
|
47
|
* {@inheritdoc}
|
48
|
*/
|
49
|
public function get_value_options() {
|
50
|
// Don't overwrite the value options.
|
51
|
}
|
52
|
|
53
|
/**
|
54
|
* {@inheritdoc}
|
55
|
*/
|
56
|
public function option_definition() {
|
57
|
$options = parent::option_definition();
|
58
|
|
59
|
$options['type'] = array('default' => 'textfield');
|
60
|
$options['limit'] = array('default' => TRUE, 'bool' => TRUE);
|
61
|
$options['vocabulary'] = array('default' => 0);
|
62
|
$options['hierarchy'] = array('default' => 0);
|
63
|
$options['error_message'] = array('default' => TRUE, 'bool' => TRUE);
|
64
|
$options['optgroups'] = array('default' => 0);
|
65
|
|
66
|
return $options;
|
67
|
}
|
68
|
|
69
|
/**
|
70
|
* {@inheritdoc}
|
71
|
*/
|
72
|
public function extra_options_form(&$form, &$form_state) {
|
73
|
$vocabularies = taxonomy_get_vocabularies();
|
74
|
$options = array();
|
75
|
foreach ($vocabularies as $voc) {
|
76
|
$options[$voc->machine_name] = check_plain($voc->name);
|
77
|
}
|
78
|
|
79
|
if ($this->options['limit']) {
|
80
|
// We only do this when the form is displayed.
|
81
|
if (empty($this->options['vocabulary'])) {
|
82
|
$first_vocabulary = reset($vocabularies);
|
83
|
$this->options['vocabulary'] = $first_vocabulary->machine_name;
|
84
|
}
|
85
|
|
86
|
if (empty($this->definition['vocabulary'])) {
|
87
|
$form['vocabulary'] = array(
|
88
|
'#type' => 'radios',
|
89
|
'#title' => t('Vocabulary'),
|
90
|
'#options' => $options,
|
91
|
'#description' => t('Select which vocabulary to show terms for in the regular options.'),
|
92
|
'#default_value' => $this->options['vocabulary'],
|
93
|
);
|
94
|
}
|
95
|
}
|
96
|
|
97
|
$form['type'] = array(
|
98
|
'#type' => 'radios',
|
99
|
'#title' => t('Selection type'),
|
100
|
'#options' => array('select' => t('Dropdown'), 'textfield' => t('Autocomplete')),
|
101
|
'#default_value' => $this->options['type'],
|
102
|
);
|
103
|
|
104
|
$form['hierarchy'] = array(
|
105
|
'#type' => 'checkbox',
|
106
|
'#title' => t('Show hierarchy in dropdown'),
|
107
|
'#default_value' => !empty($this->options['hierarchy']),
|
108
|
'#dependency' => array('radio:options[type]' => array('select')),
|
109
|
);
|
110
|
|
111
|
$form['optgroups'] = array(
|
112
|
'#type' => 'checkbox',
|
113
|
'#title' => t('Show groups in dropdown'),
|
114
|
'#default_value' => !empty($this->options['optgroups']),
|
115
|
'#dependency' => array('radio:options[type]' => array('select')),
|
116
|
);
|
117
|
|
118
|
}
|
119
|
|
120
|
/**
|
121
|
* {@inheritdoc}
|
122
|
*/
|
123
|
public function value_form(&$form, &$form_state) {
|
124
|
$vocabulary = taxonomy_vocabulary_machine_name_load($this->options['vocabulary']);
|
125
|
if (empty($vocabulary) && $this->options['limit']) {
|
126
|
$form['markup'] = array(
|
127
|
'#markup' => '<div class="form-item">' . t('An invalid vocabulary is selected. Please change it in the options.') . '</div>',
|
128
|
);
|
129
|
return;
|
130
|
}
|
131
|
|
132
|
if ($this->options['type'] == 'textfield') {
|
133
|
$default = '';
|
134
|
if ($this->value) {
|
135
|
$result = taxonomy_term_load_multiple($this->value);
|
136
|
foreach ($result as $entity_term) {
|
137
|
if ($default) {
|
138
|
$default .= ', ';
|
139
|
}
|
140
|
$default .= entity_label('taxonomy_term', $entity_term);
|
141
|
}
|
142
|
}
|
143
|
|
144
|
$form['value'] = array(
|
145
|
'#title' => $this->options['limit'] ? t('Select terms from vocabulary @voc', array('@voc' => $vocabulary->name)) : t('Select terms'),
|
146
|
'#type' => 'textfield',
|
147
|
'#default_value' => $default,
|
148
|
);
|
149
|
|
150
|
if ($this->options['limit']) {
|
151
|
$form['value']['#autocomplete_path'] = 'admin/views/ajax/autocomplete/taxonomy/' . $vocabulary->vid;
|
152
|
}
|
153
|
}
|
154
|
else {
|
155
|
$options = array();
|
156
|
if (!empty($this->options['hierarchy']) && $this->options['limit']) {
|
157
|
$tree = taxonomy_get_tree($vocabulary->vid, 0, NULL, TRUE);
|
158
|
$options = array();
|
159
|
if (!empty($tree)) {
|
160
|
if (!empty($this->options['optgroups'])) {
|
161
|
foreach ($tree as $term) {
|
162
|
$term_name = entity_label('taxonomy_term', $term);
|
163
|
if ($term->parents[0] == 0) {
|
164
|
$parent_name = $term_name;
|
165
|
}
|
166
|
else {
|
167
|
$options[$parent_name][$term->tid] = str_repeat('-', $term->depth - 1) . $term_name;
|
168
|
}
|
169
|
}
|
170
|
}
|
171
|
else {
|
172
|
foreach ($tree as $term) {
|
173
|
$options[$term->tid] = str_repeat('-', $term->depth) . entity_label('taxonomy_term', $term);
|
174
|
}
|
175
|
}
|
176
|
}
|
177
|
}
|
178
|
else {
|
179
|
$query = db_select('taxonomy_term_data', 'td');
|
180
|
$query->innerJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
|
181
|
$query->fields('td');
|
182
|
$query->orderby('tv.weight');
|
183
|
$query->orderby('tv.name');
|
184
|
$query->orderby('td.weight');
|
185
|
$query->orderby('td.name');
|
186
|
$query->addTag('taxonomy_term_access');
|
187
|
if ($this->options['limit']) {
|
188
|
$query->condition('tv.machine_name', $vocabulary->machine_name);
|
189
|
}
|
190
|
$result = $query->execute();
|
191
|
|
192
|
$tids = array();
|
193
|
foreach ($result as $term) {
|
194
|
$tids[] = $term->tid;
|
195
|
}
|
196
|
$entities = taxonomy_term_load_multiple($tids);
|
197
|
foreach ($entities as $entity_term) {
|
198
|
$options[$entity_term->tid] = entity_label('taxonomy_term', $entity_term);
|
199
|
}
|
200
|
}
|
201
|
|
202
|
$default_value = (array) $this->value;
|
203
|
|
204
|
if (!empty($form_state['exposed'])) {
|
205
|
$identifier = $this->options['expose']['identifier'];
|
206
|
|
207
|
if (!empty($this->options['expose']['reduce'])) {
|
208
|
$options = $this->reduce_value_options($options);
|
209
|
|
210
|
if (!empty($this->options['expose']['multiple']) && empty($this->options['expose']['required'])) {
|
211
|
$default_value = array();
|
212
|
}
|
213
|
}
|
214
|
|
215
|
if (empty($this->options['expose']['multiple'])) {
|
216
|
if (empty($this->options['expose']['required']) && (empty($default_value) || !empty($this->options['expose']['reduce']))) {
|
217
|
$default_value = 'All';
|
218
|
}
|
219
|
elseif (empty($default_value)) {
|
220
|
$keys = array_keys($options);
|
221
|
$default_value = array_shift($keys);
|
222
|
}
|
223
|
// Due to #1464174 there is a chance that array('') was saved in the
|
224
|
// admin UI. Let's choose a safe default value.
|
225
|
elseif ($default_value == array('')) {
|
226
|
$default_value = 'All';
|
227
|
}
|
228
|
else {
|
229
|
$copy = $default_value;
|
230
|
$default_value = array_shift($copy);
|
231
|
}
|
232
|
}
|
233
|
}
|
234
|
$form['value'] = array(
|
235
|
'#type' => 'select',
|
236
|
'#title' => $this->options['limit'] ? t('Select terms from vocabulary @voc', array('@voc' => $vocabulary->name)) : t('Select terms'),
|
237
|
'#multiple' => TRUE,
|
238
|
'#options' => $options,
|
239
|
'#size' => min(9, count($options)),
|
240
|
'#default_value' => $default_value,
|
241
|
);
|
242
|
|
243
|
if (!empty($form_state['exposed']) && isset($identifier) && !isset($form_state['input'][$identifier])) {
|
244
|
$form_state['input'][$identifier] = $default_value;
|
245
|
}
|
246
|
}
|
247
|
|
248
|
|
249
|
if (empty($form_state['exposed'])) {
|
250
|
// Retain the helper option
|
251
|
$this->helper->options_form($form, $form_state);
|
252
|
|
253
|
// Show help text if not exposed to end users.
|
254
|
$form['value']['#description'] = t('Leave blank for all. Otherwise, the first selected term will be the default instead of "Any".');
|
255
|
}
|
256
|
}
|
257
|
|
258
|
/**
|
259
|
* {@inheritdoc}
|
260
|
*/
|
261
|
public function value_validate($form, &$form_state) {
|
262
|
// We only validate if they've chosen the text field style.
|
263
|
if ($this->options['type'] != 'textfield') {
|
264
|
return;
|
265
|
}
|
266
|
|
267
|
$values = drupal_explode_tags($form_state['values']['options']['value']);
|
268
|
$tids = $this->validate_term_strings($form['value'], $values);
|
269
|
|
270
|
if ($tids) {
|
271
|
$form_state['values']['options']['value'] = $tids;
|
272
|
}
|
273
|
}
|
274
|
|
275
|
/**
|
276
|
* {@inheritdoc}
|
277
|
*/
|
278
|
public function accept_exposed_input($input) {
|
279
|
if (empty($this->options['exposed'])) {
|
280
|
return TRUE;
|
281
|
}
|
282
|
|
283
|
// We need to know the operator, which is normally set in
|
284
|
// views_handler_filter::accept_exposed_input(), before we actually call
|
285
|
// the parent version of ourselves.
|
286
|
if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id']) && isset($input[$this->options['expose']['operator_id']])) {
|
287
|
$this->operator = $input[$this->options['expose']['operator_id']];
|
288
|
}
|
289
|
|
290
|
// If view is an attachment and is inheriting exposed filters, then assume
|
291
|
// exposed input has already been validated
|
292
|
if (!empty($this->view->is_attachment) && $this->view->display_handler->uses_exposed()) {
|
293
|
$this->validated_exposed_input = (array) $this->view->exposed_raw_input[$this->options['expose']['identifier']];
|
294
|
}
|
295
|
|
296
|
// If we're checking for EMPTY or NOT, we don't need any input, and we can
|
297
|
// say that our input conditions are met by just having the right operator.
|
298
|
if ($this->operator == 'empty' || $this->operator == 'not empty') {
|
299
|
return TRUE;
|
300
|
}
|
301
|
|
302
|
// If it's non-required and there's no value don't bother filtering.
|
303
|
if (!$this->options['expose']['required'] && empty($this->validated_exposed_input)) {
|
304
|
return FALSE;
|
305
|
}
|
306
|
|
307
|
$rc = parent::accept_exposed_input($input);
|
308
|
if ($rc) {
|
309
|
// If we have previously validated input, override.
|
310
|
if (!$this->is_a_group() && isset($this->validated_exposed_input)) {
|
311
|
$this->value = $this->validated_exposed_input;
|
312
|
}
|
313
|
}
|
314
|
|
315
|
return $rc;
|
316
|
}
|
317
|
|
318
|
/**
|
319
|
* {@inheritdoc}
|
320
|
*/
|
321
|
public function exposed_validate(&$form, &$form_state) {
|
322
|
if (empty($this->options['exposed'])) {
|
323
|
return;
|
324
|
}
|
325
|
|
326
|
$identifier = $this->options['expose']['identifier'];
|
327
|
|
328
|
// We only validate if they've chosen the text field style.
|
329
|
if ($this->options['type'] != 'textfield') {
|
330
|
if (isset($form_state['values'][$identifier]) && $form_state['values'][$identifier] != 'All') {
|
331
|
$this->validated_exposed_input = (array) $form_state['values'][$identifier];
|
332
|
}
|
333
|
return;
|
334
|
}
|
335
|
|
336
|
if (empty($this->options['expose']['identifier'])) {
|
337
|
return;
|
338
|
}
|
339
|
|
340
|
$values = drupal_explode_tags($form_state['values'][$identifier]);
|
341
|
|
342
|
$tids = $this->validate_term_strings($form[$identifier], $values);
|
343
|
if ($tids) {
|
344
|
$this->validated_exposed_input = $tids;
|
345
|
}
|
346
|
}
|
347
|
|
348
|
/**
|
349
|
* Validate the user string. Since this can come from either the form
|
350
|
* or the exposed filter, this is abstracted out a bit so it can
|
351
|
* handle the multiple input sources.
|
352
|
*
|
353
|
* @param array $form
|
354
|
* The form which is used, either the views ui or the exposed filters.
|
355
|
* @param array $values
|
356
|
* The taxonomy names which will be converted to tids.
|
357
|
*
|
358
|
* @return array
|
359
|
* The taxonomy ids fo all validated terms.
|
360
|
*/
|
361
|
public function validate_term_strings(&$form, $values) {
|
362
|
if (empty($values)) {
|
363
|
return array();
|
364
|
}
|
365
|
|
366
|
$tids = array();
|
367
|
$names = array();
|
368
|
$missing = array();
|
369
|
foreach ($values as $value) {
|
370
|
$missing[strtolower($value)] = TRUE;
|
371
|
$names[] = $value;
|
372
|
}
|
373
|
|
374
|
if (!$names) {
|
375
|
return FALSE;
|
376
|
}
|
377
|
|
378
|
$query = db_select('taxonomy_term_data', 'td');
|
379
|
$query->innerJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
|
380
|
$query->fields('td');
|
381
|
$query->condition('td.name', $names);
|
382
|
$query->condition('tv.machine_name', $this->options['vocabulary']);
|
383
|
$query->addTag('taxonomy_term_access');
|
384
|
$result = $query->execute();
|
385
|
foreach ($result as $term) {
|
386
|
unset($missing[strtolower($term->name)]);
|
387
|
$tids[] = $term->tid;
|
388
|
}
|
389
|
|
390
|
if ($missing && !empty($this->options['error_message'])) {
|
391
|
form_error($form, format_plural(count($missing), 'Unable to find term: @terms', 'Unable to find terms: @terms', array('@terms' => implode(', ', array_keys($missing)))));
|
392
|
}
|
393
|
elseif ($missing && empty($this->options['error_message'])) {
|
394
|
$tids = array(0);
|
395
|
}
|
396
|
|
397
|
return $tids;
|
398
|
}
|
399
|
|
400
|
/**
|
401
|
* {@inheritdoc}
|
402
|
*/
|
403
|
public function value_submit($form, &$form_state) {
|
404
|
// prevent array_filter from messing up our arrays in parent submit.
|
405
|
}
|
406
|
|
407
|
/**
|
408
|
* {@inheritdoc}
|
409
|
*/
|
410
|
public function expose_form(&$form, &$form_state) {
|
411
|
parent::expose_form($form, $form_state);
|
412
|
if ($this->options['type'] != 'select') {
|
413
|
unset($form['expose']['reduce']);
|
414
|
}
|
415
|
$form['error_message'] = array(
|
416
|
'#type' => 'checkbox',
|
417
|
'#title' => t('Display error message'),
|
418
|
'#default_value' => !empty($this->options['error_message']),
|
419
|
);
|
420
|
}
|
421
|
|
422
|
/**
|
423
|
* {@inheritdoc}
|
424
|
*/
|
425
|
public function admin_summary() {
|
426
|
// set up $this->value_options for the parent summary
|
427
|
$this->value_options = array();
|
428
|
|
429
|
if ($this->value) {
|
430
|
$this->value = array_filter($this->value);
|
431
|
$result = taxonomy_term_load_multiple($this->value);
|
432
|
foreach ($result as $entity_term) {
|
433
|
$this->value_options[$entity_term->tid] = entity_label('taxonomy_term', $entity_term);
|
434
|
}
|
435
|
}
|
436
|
return parent::admin_summary();
|
437
|
}
|
438
|
|
439
|
}
|