1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Plugin to provide a term context.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Plugins are described by creating a $plugin array which will be used
|
10
|
* by the system that includes this file.
|
11
|
*/
|
12
|
$plugin = array(
|
13
|
'title' => t("Taxonomy term"),
|
14
|
'description' => t('A single taxonomy term object.'),
|
15
|
'context' => 'ctools_context_create_term',
|
16
|
'edit form' => 'ctools_context_term_settings_form',
|
17
|
'defaults' => array(
|
18
|
'vid' => '',
|
19
|
'tid' => '',
|
20
|
),
|
21
|
'keyword' => 'term',
|
22
|
'context name' => 'term',
|
23
|
'convert list' => array(
|
24
|
'tid' => t('Term ID'),
|
25
|
'name' => t('Term name'),
|
26
|
'name_dashed' => t('Term name, lowercased and spaces converted to dashes'),
|
27
|
'description' => t('Term Description'),
|
28
|
'vid' => t('Vocabulary ID'),
|
29
|
),
|
30
|
'convert' => 'ctools_context_term_convert',
|
31
|
// This context is deprecated and should not be usable in the UI.
|
32
|
'no ui' => TRUE,
|
33
|
'no required context ui' => TRUE,
|
34
|
);
|
35
|
|
36
|
/**
|
37
|
* It's important to remember that $conf is optional here, because contexts
|
38
|
* are not always created from the UI.
|
39
|
*/
|
40
|
function ctools_context_create_term($empty, $data = NULL, $conf = FALSE) {
|
41
|
$context = new ctools_context('term');
|
42
|
$context->plugin = 'term';
|
43
|
|
44
|
if ($empty) {
|
45
|
return $context;
|
46
|
}
|
47
|
|
48
|
if ($conf && isset($data['tid'])) {
|
49
|
$data = taxonomy_term_load($data['tid']);
|
50
|
}
|
51
|
|
52
|
if (!empty($data)) {
|
53
|
$context->data = $data;
|
54
|
$context->title = $data->name;
|
55
|
$context->argument = $data->tid;
|
56
|
$context->description = $data->description;
|
57
|
return $context;
|
58
|
}
|
59
|
}
|
60
|
|
61
|
function ctools_context_term_settings_form($form, &$form_state) {
|
62
|
$conf = $form_state['conf'];
|
63
|
|
64
|
$form['vid'] = array(
|
65
|
'#title' => t('Vocabulary'),
|
66
|
'#type' => 'select',
|
67
|
'#options' => array(),
|
68
|
'#description' => t('Select the vocabulary for this form.'),
|
69
|
'#id' => 'ctools-select-vid',
|
70
|
'#default_value' => $conf['vid'],
|
71
|
);
|
72
|
|
73
|
$description = '';
|
74
|
if (!empty($conf['tid'])) {
|
75
|
$info = db_query('SELECT * FROM {taxonomy_term_data} WHERE tid = :tid', array(':tid' => $conf['tid']))->fetchObject();
|
76
|
if ($info) {
|
77
|
$description = ' ' . t('Currently set to @term. Enter another term if you wish to change the term.', array('@term' => $info->name));
|
78
|
}
|
79
|
}
|
80
|
|
81
|
ctools_include('dependent');
|
82
|
$options = array();
|
83
|
|
84
|
$form['taxonomy']['#tree'] = TRUE;
|
85
|
|
86
|
foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
|
87
|
$options[$vid] = $vocabulary->name;
|
88
|
$form['taxonomy'][$vocabulary->vid] = array(
|
89
|
'#type' => 'textfield',
|
90
|
'#description' => t('Select a term from @vocabulary.', array('@vocabulary' => $vocabulary->name)) . $description,
|
91
|
'#autocomplete_path' => 'taxonomy/autocomplete/' . $vocabulary->vid,
|
92
|
'#dependency' => array('ctools-select-vid' => array($vocabulary->vid)),
|
93
|
);
|
94
|
|
95
|
}
|
96
|
|
97
|
$form['vid']['#options'] = $options;
|
98
|
|
99
|
$form['tid'] = array(
|
100
|
'#type' => 'value',
|
101
|
'#value' => $conf['tid'],
|
102
|
);
|
103
|
|
104
|
$form['set_identifier'] = array(
|
105
|
'#type' => 'checkbox',
|
106
|
'#default_value' => FALSE,
|
107
|
'#title' => t('Reset identifier to term title'),
|
108
|
'#description' => t('If checked, the identifier will be reset to the term name of the selected term.'),
|
109
|
);
|
110
|
|
111
|
return $form;
|
112
|
}
|
113
|
|
114
|
/**
|
115
|
* Validate a term.
|
116
|
*/
|
117
|
function ctools_context_term_settings_form_validate($form, &$form_state) {
|
118
|
// Validate the autocomplete.
|
119
|
$vid = $form_state['values']['vid'];
|
120
|
if (empty($form_state['values']['tid']) && empty($form_state['values']['taxonomy'][$vid])) {
|
121
|
form_error($form['taxonomy'][$vid], t('You must select a term.'));
|
122
|
return;
|
123
|
}
|
124
|
|
125
|
if (empty($form_state['values']['taxonomy'][$vid])) {
|
126
|
return;
|
127
|
}
|
128
|
|
129
|
$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();
|
130
|
|
131
|
if (!$term) {
|
132
|
form_error($form['taxonomy'][$vid], t('Invalid term selected.'));
|
133
|
}
|
134
|
else {
|
135
|
form_set_value($form['tid'], $term->tid, $form_state);
|
136
|
}
|
137
|
}
|
138
|
|
139
|
function ctools_context_term_settings_form_submit($form, &$form_state) {
|
140
|
if ($form_state['values']['set_identifier']) {
|
141
|
$term = db_query('SELECT tid, name FROM {taxonomy_term_data} WHERE LOWER(tid) = :tid', array(':tid' => $form_state['values']['tid']))->fetchObject();
|
142
|
$form_state['values']['identifier'] = $term->name;
|
143
|
}
|
144
|
|
145
|
$form_state['conf']['tid'] = $form_state['values']['tid'];
|
146
|
$form_state['conf']['vid'] = $form_state['values']['vid'];
|
147
|
}
|
148
|
|
149
|
/**
|
150
|
* Convert a context into a string.
|
151
|
*/
|
152
|
function ctools_context_term_convert($context, $type) {
|
153
|
switch ($type) {
|
154
|
case 'tid':
|
155
|
return $context->data->tid;
|
156
|
|
157
|
case 'name':
|
158
|
return $context->data->name;
|
159
|
|
160
|
case 'name_dashed':
|
161
|
return drupal_strtolower(str_replace(' ', '-', $context->data->name));
|
162
|
|
163
|
case 'vid':
|
164
|
return $context->data->vid;
|
165
|
|
166
|
case 'description':
|
167
|
return $context->data->description;
|
168
|
}
|
169
|
}
|