1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Definition of views_handler_relationship_node_term_data.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Relationship handler to return the taxonomy terms of nodes.
|
10
|
*
|
11
|
* @ingroup views_relationship_handlers
|
12
|
*/
|
13
|
class views_handler_relationship_node_term_data extends views_handler_relationship {
|
14
|
|
15
|
/**
|
16
|
* {@inheritdoc}
|
17
|
*/
|
18
|
public function init(&$view, &$options) {
|
19
|
parent::init($view, $options);
|
20
|
|
21
|
// Convert legacy vids option to machine name vocabularies.
|
22
|
if (!empty($this->options['vids'])) {
|
23
|
$vocabularies = taxonomy_get_vocabularies();
|
24
|
foreach ($this->options['vids'] as $vid) {
|
25
|
if (isset($vocabularies[$vid], $vocabularies[$vid]->machine_name)) {
|
26
|
$this->options['vocabularies'][$vocabularies[$vid]->machine_name] = $vocabularies[$vid]->machine_name;
|
27
|
}
|
28
|
}
|
29
|
}
|
30
|
}
|
31
|
|
32
|
/**
|
33
|
* {@inheritdoc}
|
34
|
*/
|
35
|
public function option_definition() {
|
36
|
$options = parent::option_definition();
|
37
|
$options['vocabularies'] = array('default' => array());
|
38
|
return $options;
|
39
|
}
|
40
|
|
41
|
/**
|
42
|
* {@inheritdoc}
|
43
|
*/
|
44
|
public function options_form(&$form, &$form_state) {
|
45
|
$vocabularies = taxonomy_get_vocabularies();
|
46
|
$options = array();
|
47
|
foreach ($vocabularies as $voc) {
|
48
|
$options[$voc->machine_name] = check_plain($voc->name);
|
49
|
}
|
50
|
|
51
|
$form['vocabularies'] = array(
|
52
|
'#type' => 'checkboxes',
|
53
|
'#title' => t('Vocabularies'),
|
54
|
'#options' => $options,
|
55
|
'#default_value' => $this->options['vocabularies'],
|
56
|
'#description' => t('Choose which vocabularies you wish to relate. Remember that every term found will create a new record, so this relationship is best used on just one vocabulary that has only one term per node.'),
|
57
|
);
|
58
|
parent::options_form($form, $form_state);
|
59
|
}
|
60
|
|
61
|
/**
|
62
|
* Called to implement a relationship in a query.
|
63
|
*/
|
64
|
public function query() {
|
65
|
$this->ensure_my_table();
|
66
|
|
67
|
$def = $this->definition;
|
68
|
$def['table'] = 'taxonomy_term_data';
|
69
|
|
70
|
if (!array_filter($this->options['vocabularies'])) {
|
71
|
$taxonomy_index = $this->query->add_table('taxonomy_index', $this->relationship);
|
72
|
$def['left_table'] = $taxonomy_index;
|
73
|
$def['left_field'] = 'tid';
|
74
|
$def['field'] = 'tid';
|
75
|
$def['type'] = empty($this->options['required']) ? 'LEFT' : 'INNER';
|
76
|
}
|
77
|
else {
|
78
|
// If vocabularies are supplied join a subselect instead.
|
79
|
$def['left_table'] = $this->table_alias;
|
80
|
$def['left_field'] = 'nid';
|
81
|
$def['field'] = 'nid';
|
82
|
$def['type'] = empty($this->options['required']) ? 'LEFT' : 'INNER';
|
83
|
|
84
|
$query = db_select('taxonomy_term_data', 'td');
|
85
|
$query->addJoin($def['type'], 'taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
|
86
|
$query->addJoin($def['type'], 'taxonomy_index', 'tn', 'tn.tid = td.tid');
|
87
|
$query->condition('tv.machine_name', array_filter($this->options['vocabularies']));
|
88
|
if (empty($this->query->options['disable_sql_rewrite'])) {
|
89
|
$query->addTag('taxonomy_term_access');
|
90
|
}
|
91
|
$query->fields('td');
|
92
|
$query->fields('tn', array('nid'));
|
93
|
$def['table formula'] = $query;
|
94
|
}
|
95
|
|
96
|
$join = new views_join();
|
97
|
|
98
|
$join->definition = $def;
|
99
|
$join->construct();
|
100
|
$join->adjusted = TRUE;
|
101
|
|
102
|
// Use a short alias for this.
|
103
|
$alias = $def['table'] . '_' . $this->table;
|
104
|
|
105
|
$this->alias = $this->query->add_relationship($alias, $join, 'taxonomy_term_data', $this->relationship);
|
106
|
}
|
107
|
|
108
|
}
|