term.inc
4.78 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
164
165
166
<?php
/**
* @file
*
* Plugin to provide a term context
*/
/**
* 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"),
'description' => t('A single taxonomy term object.'),
'context' => 'ctools_context_create_term',
'edit form' => 'ctools_context_term_settings_form',
'defaults' => array(
'vid' => '',
'tid' => '',
),
'keyword' => 'term',
'context name' => 'term',
'convert list' => array(
'tid' => t('Term ID'),
'name' => t('Term name'),
'name_dashed' => t('Term name, lowercased and spaces converted to dashes'),
'description' => t('Term Description'),
'vid' => t('Vocabulary ID'),
),
'convert' => 'ctools_context_term_convert',
// This context is deprecated and should not be usable in the UI.
'no ui' => TRUE,
'no required context ui' => TRUE,
);
/**
* It's important to remember that $conf is optional here, because contexts
* are not always created from the UI.
*/
function ctools_context_create_term($empty, $data = NULL, $conf = FALSE) {
$context = new ctools_context('term');
$context->plugin = 'term';
if ($empty) {
return $context;
}
if ($conf && isset($data['tid'])) {
$data = taxonomy_term_load($data['tid']);
}
if (!empty($data)) {
$context->data = $data;
$context->title = $data->name;
$context->argument = $data->tid;
$context->description = $data->description;
return $context;
}
}
function ctools_context_term_settings_form($form, &$form_state) {
$conf = $form_state['conf'];
$form['vid'] = array(
'#title' => t('Vocabulary'),
'#type' => 'select',
'#options' => array(),
'#description' => t('Select the vocabulary for this form.'),
'#id' => 'ctools-select-vid',
'#default_value' => $conf['vid'],
);
$description = '';
if (!empty($conf['tid'])) {
$info = db_query('SELECT * FROM {taxonomy_term_data} WHERE tid = :tid', array(':tid' => $conf['tid']))->fetchObject();
if ($info) {
$description = ' ' . t('Currently set to @term. Enter another term if you wish to change the term.', array('@term' => $info->name));
}
}
ctools_include('dependent');
$options = array();
$form['taxonomy']['#tree'] = TRUE;
foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
$options[$vid] = $vocabulary->name;
$form['taxonomy'][$vocabulary->vid] = array(
'#type' => 'textfield',
'#description' => t('Select a term from @vocabulary.', array('@vocabulary' => $vocabulary->name)) . $description,
'#autocomplete_path' => 'taxonomy/autocomplete/' . $vocabulary->vid,
'#dependency' => array('ctools-select-vid' => array($vocabulary->vid)),
);
}
$form['vid']['#options'] = $options;
$form['tid'] = array(
'#type' => 'value',
'#value' => $conf['tid'],
);
$form['set_identifier'] = array(
'#type' => 'checkbox',
'#default_value' => FALSE,
'#title' => t('Reset identifier to term title'),
'#description' => t('If checked, the identifier will be reset to the term name of the selected term.'),
);
return $form;
}
/**
* Validate a term.
*/
function ctools_context_term_settings_form_validate($form, &$form_state) {
// Validate the autocomplete
$vid = $form_state['values']['vid'];
if (empty($form_state['values']['tid']) && empty($form_state['values']['taxonomy'][$vid])) {
form_error($form['taxonomy'][$vid], t('You must select a term.'));
return;
}
if (empty($form_state['values']['taxonomy'][$vid])) {
return;
}
$term = db_query('SELECT tid FROM {taxonomy_term_data} WHERE LOWER(name) = LOWER(:name) AND vid = :vid', array(':name' => $form_state['values']['taxonomy'][$vid], ':vid' => $vid))->fetchObject();
if (!$term) {
form_error($form['taxonomy'][$vid], t('Invalid term selected.'));
}
else {
form_set_value($form['tid'], $term->tid, $form_state);
}
}
function ctools_context_term_settings_form_submit($form, &$form_state) {
if ($form_state['values']['set_identifier']) {
$term = db_query('SELECT tid, name FROM {taxonomy_term_data} WHERE LOWER(tid) = :tid', array(':tid' => $form_state['values']['tid']))->fetchObject();
$form_state['values']['identifier'] = $term->name;
}
$form_state['conf']['tid'] = $form_state['values']['tid'];
$form_state['conf']['vid'] = $form_state['values']['vid'];
}
/**
* Convert a context into a string.
*/
function ctools_context_term_convert($context, $type) {
switch ($type) {
case 'tid':
return $context->data->tid;
case 'name':
return $context->data->name;
case 'name_dashed':
return drupal_strtolower(str_replace(' ', '-', $context->data->name));
case 'vid':
return $context->data->vid;
case 'description':
return $context->data->description;
}
}