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