6 * Plugin to provide a terms context
10 * Plugins are described by creating a $plugin array which will be used
11 * by the system that includes this file.
14 'title' => t("Taxonomy terms"),
15 'description' => t('Multiple taxonomy terms, as a group.'),
16 'context' => 'ctools_context_create_terms',
18 // This context is deprecated and should not be usable in the UI.
20 'context name' => 'terms',
21 'convert list' => array(
22 'tid' => t('Term ID of first term'),
23 'tids' => t('Term ID of all term, separated by + or ,'),
24 'name' => t('Term name of first term'),
25 'name_dashed' => t('Term name of first term, lowercased and spaces converted to dashes'),
26 'names' => t('Term name of all terms, separated by + or ,'),
27 'names_dashed' => t('Term name of all terms, separated by + or , and lowercased and spaces converted to dashes'),
28 'vid' => t('Vocabulary ID of first term'),
30 'convert' => 'ctools_context_terms_convert',
34 * It's important to remember that $conf is optional here, because contexts
35 * are not always created from the UI.
37 function ctools_context_create_terms($empty, $data = NULL, $conf = FALSE) {
38 // The input is expected to be an object as created by ctools_break_phrase
39 // which contains a group of terms.
41 $context = new ctools_context(array('terms', 'entity:taxonomy_term'));
42 $context->plugin = 'terms';
48 if (!empty($data) && is_object($data)) {
49 $context->operator = $data->operator;
50 $context->tids = $data->value;
51 if (!isset($data->term)) {
52 // load the first term:
53 reset($context->tids);
54 $data->term = taxonomy_term_load(current($context->tids));
56 $context->data = $data->term;
57 $context->title = $data->term->name;
58 $context->argument = implode($context->operator == 'or' ? '+' : ',', array_unique($context->tids));
64 * Convert a context into a string.
66 function ctools_context_terms_convert($context, $type) {
69 return $context->data->tid;
71 return $context->argument;
73 return $context->data->name;
75 return drupal_strtolower(str_replace(' ', '-', $context->data->name));
78 // We only run this query if this item was requested:
79 if (!isset($context->names)) {
80 if (empty($context->tids)) {
84 $result = db_query('SELECT tid, name FROM {taxonomy_term_data} WHERE tid IN (:tids)', array(':tids' => $context->tids));
85 foreach ($result as $term) {
86 $names[$term->tid] = $term->name;
87 if ($type == 'names_dashed') {
88 $names[$term->tid] = drupal_strtolower(str_replace(' ', '-', $names[$term->tid]));
91 $context->names = implode($context->operator == 'or' ? ' + ' : ', ', $names);
94 return $context->names;
96 return $context->data->vid;