1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
*
|
6
|
* Plugin to provide an argument handler for a Taxonomy term
|
7
|
*/
|
8
|
|
9
|
/**
|
10
|
* Plugins are described by creating a $plugin array which will be used
|
11
|
* by the system that includes this file.
|
12
|
*/
|
13
|
$plugin = array(
|
14
|
'title' => t("Taxonomy term: ID"),
|
15
|
// keyword to use for %substitution
|
16
|
'keyword' => 'term',
|
17
|
'description' => t('Creates a single taxonomy term from a taxonomy ID or taxonomy term name.'),
|
18
|
'context' => 'ctools_term_context',
|
19
|
'default' => array('input_form' => 'tid', 'breadcrumb' => TRUE, 'transform' => FALSE),
|
20
|
'settings form' => 'ctools_term_settings_form',
|
21
|
'settings form validate' => 'ctools_term_settings_form_validate',
|
22
|
'placeholder form' => 'ctools_term_ctools_argument_placeholder',
|
23
|
'breadcrumb' => 'ctools_term_breadcrumb',
|
24
|
);
|
25
|
|
26
|
/**
|
27
|
* Discover if this argument gives us the term we crave.
|
28
|
*/
|
29
|
function ctools_term_context($arg = NULL, $conf = NULL, $empty = FALSE) {
|
30
|
// If unset it wants a generic, unfilled context.
|
31
|
if ($empty) {
|
32
|
return ctools_context_create_empty('entity:taxonomy_term');
|
33
|
}
|
34
|
|
35
|
if (isset($conf['vocabularies'])) {
|
36
|
$vocabularies = $conf['vocabularies'];
|
37
|
}
|
38
|
else {
|
39
|
$vids = isset($conf['vids']) ? $conf['vids'] : array();
|
40
|
|
41
|
// Convert legacy use of vids to machine names.
|
42
|
$vocabularies = _ctools_term_vocabulary_machine_name_convert($vids);
|
43
|
}
|
44
|
|
45
|
if (is_object($arg)) {
|
46
|
$term = $arg;
|
47
|
}
|
48
|
else {
|
49
|
switch ($conf['input_form']) {
|
50
|
case 'tid':
|
51
|
default:
|
52
|
if (!is_numeric($arg)) {
|
53
|
return FALSE;
|
54
|
}
|
55
|
$term = taxonomy_term_load($arg);
|
56
|
break;
|
57
|
|
58
|
case 'term':
|
59
|
if (!empty($conf['transform'])) {
|
60
|
$arg = strtr($arg, '-', ' ');
|
61
|
}
|
62
|
|
63
|
$terms = taxonomy_get_term_by_name($arg);
|
64
|
// If only one term is found, fall through to vocabulary check below.
|
65
|
if ((count($terms) > 1) && $vocabularies) {
|
66
|
foreach ($terms as $potential) {
|
67
|
foreach ($vocabularies as $machine_name) {
|
68
|
if ($potential->vocabulary_machine_name == $machine_name) {
|
69
|
$term = $potential;
|
70
|
// break out of the foreaches AND the case
|
71
|
break 3;
|
72
|
}
|
73
|
}
|
74
|
}
|
75
|
}
|
76
|
$term = array_shift($terms);
|
77
|
break;
|
78
|
}
|
79
|
|
80
|
if (empty($term)) {
|
81
|
return NULL;
|
82
|
}
|
83
|
}
|
84
|
|
85
|
if ($vocabularies && !isset($vocabularies[$term->vocabulary_machine_name])) {
|
86
|
return NULL;
|
87
|
}
|
88
|
|
89
|
$context = ctools_context_create('entity:taxonomy_term', $term);
|
90
|
$context->original_argument = $arg;
|
91
|
return $context;
|
92
|
}
|
93
|
|
94
|
/**
|
95
|
* Settings form for the argument
|
96
|
*/
|
97
|
function ctools_term_settings_form(&$form, &$form_state, $conf) {
|
98
|
// @todo allow synonym use like Views does.
|
99
|
$form['settings']['input_form'] = array(
|
100
|
'#title' => t('Argument type'),
|
101
|
'#type' => 'radios',
|
102
|
'#options' => array('tid' => t('Term ID'), 'term' => t('Term name')),
|
103
|
'#default_value' => $conf['input_form'],
|
104
|
'#prefix' => '<div class="clearfix">',
|
105
|
'#suffix' => '</div>',
|
106
|
);
|
107
|
|
108
|
$vocabularies = taxonomy_get_vocabularies();
|
109
|
$options = array();
|
110
|
foreach ($vocabularies as $vid => $vocab) {
|
111
|
$options[$vocab->machine_name] = $vocab->name;
|
112
|
}
|
113
|
|
114
|
// Fallback on legacy 'vids', when no vocabularies are available.
|
115
|
if (empty($conf['vocabularies']) && !empty($conf['vids'])) {
|
116
|
$conf['vocabularies'] = _ctools_term_vocabulary_machine_name_convert(array_filter($conf['vids']));
|
117
|
unset($conf['vids']);
|
118
|
}
|
119
|
|
120
|
$form['settings']['vocabularies'] = array(
|
121
|
'#title' => t('Limit to these vocabularies'),
|
122
|
'#type' => 'checkboxes',
|
123
|
'#options' => $options,
|
124
|
'#default_value' => !empty($conf['vocabularies']) ? $conf['vocabularies'] : array(),
|
125
|
'#description' => t('If no vocabularies are checked, terms from all vocabularies will be accepted.'),
|
126
|
);
|
127
|
|
128
|
$form['settings']['breadcrumb'] = array(
|
129
|
'#title' => t('Inject hierarchy into breadcrumb trail'),
|
130
|
'#type' => 'checkbox',
|
131
|
'#default_value' => !empty($conf['breadcrumb']),
|
132
|
'#description' => t('If checked, taxonomy term parents will appear in the breadcrumb trail.'),
|
133
|
);
|
134
|
|
135
|
$form['settings']['transform'] = array(
|
136
|
'#title' => t('Transform dashes in URL to spaces in term name filter values'),
|
137
|
'#type' => 'checkbox',
|
138
|
'#default_value' => !empty($conf['transform']),
|
139
|
);
|
140
|
// return $form;
|
141
|
}
|
142
|
|
143
|
function ctools_term_settings_form_validate (&$form, &$form_state) {
|
144
|
// Filter the selected vocabularies to avoid storing redundant data.
|
145
|
$vocabularies = array_filter($form_state['values']['settings']['vocabularies']);
|
146
|
form_set_value($form['settings']['vocabularies'], $vocabularies, $form_state);
|
147
|
}
|
148
|
|
149
|
/**
|
150
|
* Form fragment to get an argument to convert a placeholder for preview.
|
151
|
*/
|
152
|
function ctools_term_ctools_argument_placeholder($conf) {
|
153
|
switch ($conf['input_form']) {
|
154
|
case 'tid':
|
155
|
default:
|
156
|
return array(
|
157
|
'#type' => 'textfield',
|
158
|
'#description' => t('Enter a taxonomy term ID.'),
|
159
|
);
|
160
|
case 'term':
|
161
|
return array(
|
162
|
'#type' => 'textfield',
|
163
|
'#description' => t('Enter a taxonomy term name.'),
|
164
|
);
|
165
|
}
|
166
|
}
|
167
|
|
168
|
/**
|
169
|
* Inject the breadcrumb trail if necessary.
|
170
|
*/
|
171
|
function ctools_term_breadcrumb($conf, $context) {
|
172
|
if (empty($conf['breadcrumb']) || empty($context->data) || empty($context->data->tid)) {
|
173
|
return;
|
174
|
}
|
175
|
|
176
|
$breadcrumb = array();
|
177
|
$current = new stdClass();
|
178
|
$current->tid = $context->data->tid;
|
179
|
while ($parents = taxonomy_get_parents($current->tid)) {
|
180
|
$current = array_shift($parents);
|
181
|
$breadcrumb[] = l($current->name, 'taxonomy/term/' . $current->tid);
|
182
|
}
|
183
|
|
184
|
$breadcrumb = array_merge(drupal_get_breadcrumb(), array_reverse($breadcrumb));
|
185
|
drupal_set_breadcrumb($breadcrumb);
|
186
|
}
|
187
|
|
188
|
/**
|
189
|
* Helper function to convert convert legacy vocabulary ids into machine names.
|
190
|
*
|
191
|
* @param array $vids
|
192
|
* Array of either vids.
|
193
|
* @return array
|
194
|
* A keyed array of machine names.
|
195
|
*/
|
196
|
function _ctools_term_vocabulary_machine_name_convert($vids) {
|
197
|
$vocabularies = taxonomy_vocabulary_load_multiple($vids);
|
198
|
$return = array();
|
199
|
foreach($vocabularies as $vocabulary) {
|
200
|
$return[$vocabulary->machine_name] = $vocabulary->machine_name;
|
201
|
}
|
202
|
return $return;
|
203
|
}
|