1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Definition of views_handler_field_term_node_tid.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Field handler to display all taxonomy terms of a node.
|
10
|
*
|
11
|
* @ingroup views_field_handlers
|
12
|
*/
|
13
|
class views_handler_field_term_node_tid extends views_handler_field_prerender_list {
|
14
|
|
15
|
/**
|
16
|
* {@inheritdoc}
|
17
|
*/
|
18
|
public function init(&$view, &$options) {
|
19
|
parent::init($view, $options);
|
20
|
// @todo Wouldn't it be possible to use $this->base_table and no if here?
|
21
|
if ($view->base_table == 'node_revision') {
|
22
|
$this->additional_fields['nid'] = array('table' => 'node_revision', 'field' => 'nid');
|
23
|
}
|
24
|
else {
|
25
|
$this->additional_fields['nid'] = array('table' => 'node', 'field' => 'nid');
|
26
|
}
|
27
|
|
28
|
// Convert legacy vids option to machine name vocabularies.
|
29
|
if (!empty($this->options['vids'])) {
|
30
|
$vocabularies = taxonomy_get_vocabularies();
|
31
|
foreach ($this->options['vids'] as $vid) {
|
32
|
if (isset($vocabularies[$vid], $vocabularies[$vid]->machine_name)) {
|
33
|
$this->options['vocabularies'][$vocabularies[$vid]->machine_name] = $vocabularies[$vid]->machine_name;
|
34
|
}
|
35
|
}
|
36
|
}
|
37
|
}
|
38
|
|
39
|
/**
|
40
|
* {@inheritdoc}
|
41
|
*/
|
42
|
public function option_definition() {
|
43
|
$options = parent::option_definition();
|
44
|
|
45
|
$options['link_to_taxonomy'] = array('default' => TRUE, 'bool' => TRUE);
|
46
|
$options['limit'] = array('default' => FALSE, 'bool' => TRUE);
|
47
|
$options['vocabularies'] = array('default' => array());
|
48
|
|
49
|
return $options;
|
50
|
}
|
51
|
|
52
|
/**
|
53
|
* Provide "link to term" option.
|
54
|
*/
|
55
|
public function options_form(&$form, &$form_state) {
|
56
|
$form['link_to_taxonomy'] = array(
|
57
|
'#title' => t('Link this field to its term page'),
|
58
|
'#type' => 'checkbox',
|
59
|
'#default_value' => !empty($this->options['link_to_taxonomy']),
|
60
|
);
|
61
|
|
62
|
$form['limit'] = array(
|
63
|
'#type' => 'checkbox',
|
64
|
'#title' => t('Limit terms by vocabulary'),
|
65
|
'#default_value' => $this->options['limit'],
|
66
|
);
|
67
|
|
68
|
$options = array();
|
69
|
$vocabularies = taxonomy_get_vocabularies();
|
70
|
foreach ($vocabularies as $voc) {
|
71
|
$options[$voc->machine_name] = check_plain($voc->name);
|
72
|
}
|
73
|
|
74
|
$form['vocabularies'] = array(
|
75
|
'#prefix' => '<div><div id="edit-options-vocabularies">',
|
76
|
'#suffix' => '</div></div>',
|
77
|
'#type' => 'checkboxes',
|
78
|
'#title' => t('Vocabularies'),
|
79
|
'#options' => $options,
|
80
|
'#default_value' => $this->options['vocabularies'],
|
81
|
'#dependency' => array('edit-options-limit' => array(TRUE)),
|
82
|
);
|
83
|
|
84
|
parent::options_form($form, $form_state);
|
85
|
}
|
86
|
|
87
|
/**
|
88
|
* Add this term to the query.
|
89
|
*/
|
90
|
public function query() {
|
91
|
$this->add_additional_fields();
|
92
|
}
|
93
|
|
94
|
/**
|
95
|
* {@inheritdoc}
|
96
|
*/
|
97
|
public function pre_render(&$values) {
|
98
|
$this->field_alias = $this->aliases['nid'];
|
99
|
$nids = array();
|
100
|
foreach ($values as $result) {
|
101
|
if (!empty($result->{$this->aliases['nid']})) {
|
102
|
$nids[] = $result->{$this->aliases['nid']};
|
103
|
}
|
104
|
}
|
105
|
|
106
|
if ($nids) {
|
107
|
$query = db_select('taxonomy_term_data', 'td');
|
108
|
$query->innerJoin('taxonomy_index', 'tn', 'td.tid = tn.tid');
|
109
|
$query->innerJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
|
110
|
$query->fields('td');
|
111
|
$query->addField('tn', 'nid', 'node_nid');
|
112
|
$query->addField('tv', 'name', 'vocabulary');
|
113
|
$query->addField('tv', 'machine_name', 'vocabulary_machine_name');
|
114
|
$query->orderby('td.weight');
|
115
|
$query->orderby('td.name');
|
116
|
$query->condition('tn.nid', $nids);
|
117
|
$query->addTag('taxonomy_term_access');
|
118
|
$vocabs = array_filter($this->options['vocabularies']);
|
119
|
if (!empty($this->options['limit']) && !empty($vocabs)) {
|
120
|
$query->condition('tv.machine_name', $vocabs);
|
121
|
}
|
122
|
$result = $query->execute();
|
123
|
|
124
|
foreach ($result as $term) {
|
125
|
$this->items[$term->node_nid][$term->tid]['name'] = check_plain($term->name);
|
126
|
$this->items[$term->node_nid][$term->tid]['tid'] = $term->tid;
|
127
|
$this->items[$term->node_nid][$term->tid]['vocabulary_machine_name'] = check_plain($term->vocabulary_machine_name);
|
128
|
$this->items[$term->node_nid][$term->tid]['vocabulary'] = check_plain($term->vocabulary);
|
129
|
|
130
|
if (!empty($this->options['link_to_taxonomy'])) {
|
131
|
$this->items[$term->node_nid][$term->tid]['make_link'] = TRUE;
|
132
|
$this->items[$term->node_nid][$term->tid]['path'] = 'taxonomy/term/' . $term->tid;
|
133
|
}
|
134
|
}
|
135
|
}
|
136
|
}
|
137
|
|
138
|
/**
|
139
|
* {@inheritdoc}
|
140
|
*/
|
141
|
public function render_item($count, $item) {
|
142
|
return $item['name'];
|
143
|
}
|
144
|
|
145
|
/**
|
146
|
* {@inheritdoc}
|
147
|
*/
|
148
|
public function document_self_tokens(&$tokens) {
|
149
|
$tokens['[' . $this->options['id'] . '-tid' . ']'] = t('The taxonomy term ID for the term.');
|
150
|
$tokens['[' . $this->options['id'] . '-name' . ']'] = t('The taxonomy term name for the term.');
|
151
|
$tokens['[' . $this->options['id'] . '-vocabulary-machine-name' . ']'] = t('The machine name for the vocabulary the term belongs to.');
|
152
|
$tokens['[' . $this->options['id'] . '-vocabulary' . ']'] = t('The name for the vocabulary the term belongs to.');
|
153
|
}
|
154
|
|
155
|
/**
|
156
|
* {@inheritdoc}
|
157
|
*/
|
158
|
public function add_self_tokens(&$tokens, $item) {
|
159
|
foreach (array('tid', 'name', 'vocabulary_machine_name', 'vocabulary') as $token) {
|
160
|
// Replace _ with - for the vocabulary machine name.
|
161
|
$tokens['[' . $this->options['id'] . '-' . str_replace('_', '-', $token) . ']'] = isset($item[$token]) ? $item[$token] : '';
|
162
|
}
|
163
|
}
|
164
|
|
165
|
}
|