comparison sites/all/modules/ctools/plugins/arguments/term.inc @ 0:ff03f76ab3fe

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