Projet

Général

Profil

Paste
Télécharger (6,02 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / plugins / arguments / term.inc @ c304a780

1
<?php
2

    
3
/**
4
 * @file
5
 * Plugin to provide an argument handler for a Taxonomy term.
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: ID"),
14
  // Keyword to use for %substitution.
15
  'keyword' => 'term',
16
  'description' => t('Creates a single taxonomy term from a taxonomy ID or taxonomy term name.'),
17
  'context' => 'ctools_term_context',
18
  'default' => array('input_form' => 'tid', 'breadcrumb' => TRUE, 'transform' => FALSE),
19
  'settings form' => 'ctools_term_settings_form',
20
  'settings form validate' => 'ctools_term_settings_form_validate',
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 (isset($conf['vocabularies'])) {
35
    $vocabularies = $conf['vocabularies'];
36
  }
37
  else {
38
    $vids = isset($conf['vids']) ? $conf['vids'] : array();
39

    
40
    // Convert legacy use of vids to machine names.
41
    $vocabularies = _ctools_term_vocabulary_machine_name_convert($vids);
42
  }
43

    
44
  if (is_object($arg)) {
45
    $term = $arg;
46
  }
47
  else {
48
    switch ($conf['input_form']) {
49
      case 'tid':
50
      default:
51
        if (!is_numeric($arg)) {
52
          return FALSE;
53
        }
54
        $term = taxonomy_term_load($arg);
55
        break;
56

    
57
      case 'term':
58
        if (!empty($conf['transform'])) {
59
          $arg = strtr($arg, '-', ' ');
60
        }
61

    
62
        $terms = taxonomy_get_term_by_name($arg);
63
        // If only one term is found, fall through to vocabulary check below.
64
        if ((count($terms) > 1) && $vocabularies) {
65
          foreach ($terms as $potential) {
66
            foreach ($vocabularies as $machine_name) {
67
              if ($potential->vocabulary_machine_name == $machine_name) {
68
                $term = $potential;
69
                // Break out of the foreaches AND the case.
70
                break 3;
71
              }
72
            }
73
          }
74
        }
75
        $term = array_shift($terms);
76
        break;
77
    }
78

    
79
    if (empty($term)) {
80
      return NULL;
81
    }
82
  }
83

    
84
  if ($vocabularies && !isset($vocabularies[$term->vocabulary_machine_name])) {
85
    return NULL;
86
  }
87

    
88
  $context = ctools_context_create('entity:taxonomy_term', $term);
89
  $context->original_argument = $arg;
90
  return $context;
91
}
92

    
93
/**
94
 * Settings form for the argument.
95
 */
96
function ctools_term_settings_form(&$form, &$form_state, $conf) {
97
  // @todo allow synonym use like Views does.
98
  $form['settings']['input_form'] = array(
99
    '#title' => t('Argument type'),
100
    '#type' => 'radios',
101
    '#options' => array('tid' => t('Term ID'), 'term' => t('Term name')),
102
    '#default_value' => $conf['input_form'],
103
    '#prefix' => '<div class="clearfix">',
104
    '#suffix' => '</div>',
105
  );
106

    
107
  $vocabularies = taxonomy_get_vocabularies();
108
  $options = array();
109
  foreach ($vocabularies as $vid => $vocab) {
110
    $options[$vocab->machine_name] = $vocab->name;
111
  }
112

    
113
  // Fallback on legacy 'vids', when no vocabularies are available.
114
  if (empty($conf['vocabularies']) && !empty($conf['vids'])) {
115
    $conf['vocabularies'] = _ctools_term_vocabulary_machine_name_convert(array_filter($conf['vids']));
116
    unset($conf['vids']);
117
  }
118

    
119
  $form['settings']['vocabularies'] = array(
120
    '#title' => t('Limit to these vocabularies'),
121
    '#type' => 'checkboxes',
122
    '#options' => $options,
123
    '#default_value' => !empty($conf['vocabularies']) ? $conf['vocabularies'] : array(),
124
    '#description' => t('If no vocabularies are checked, terms from all vocabularies will be accepted.'),
125
  );
126

    
127
  $form['settings']['breadcrumb'] = array(
128
    '#title' => t('Inject hierarchy into breadcrumb trail'),
129
    '#type' => 'checkbox',
130
    '#default_value' => !empty($conf['breadcrumb']),
131
    '#description' => t('If checked, taxonomy term parents will appear in the breadcrumb trail.'),
132
  );
133

    
134
  $form['settings']['transform'] = array(
135
    '#title' => t('Transform dashes in URL to spaces in term name filter values'),
136
    '#type' => 'checkbox',
137
    '#default_value' => !empty($conf['transform']),
138
  );
139
}
140

    
141
function ctools_term_settings_form_validate(&$form, &$form_state) {
142
  // Filter the selected vocabularies to avoid storing redundant data.
143
  $vocabularies = array_filter($form_state['values']['settings']['vocabularies']);
144
  form_set_value($form['settings']['vocabularies'], $vocabularies, $form_state);
145
}
146

    
147
/**
148
 * Form fragment to get an argument to convert a placeholder for preview.
149
 */
150
function ctools_term_ctools_argument_placeholder($conf) {
151
  switch ($conf['input_form']) {
152
    case 'tid':
153
    default:
154
      return array(
155
        '#type' => 'textfield',
156
        '#description' => t('Enter a taxonomy term ID.'),
157
      );
158

    
159
    case 'term':
160
      return array(
161
        '#type' => 'textfield',
162
        '#description' => t('Enter a taxonomy term name.'),
163
      );
164
  }
165
}
166

    
167
/**
168
 * Inject the breadcrumb trail if necessary.
169
 */
170
function ctools_term_breadcrumb($conf, $context) {
171
  if (empty($conf['breadcrumb']) || empty($context->data) || empty($context->data->tid)) {
172
    return;
173
  }
174

    
175
  $breadcrumb = array();
176
  $current = new stdClass();
177
  $current->tid = $context->data->tid;
178
  while ($parents = taxonomy_get_parents($current->tid)) {
179
    $current = array_shift($parents);
180
    $breadcrumb[] = l($current->name, 'taxonomy/term/' . $current->tid);
181
  }
182

    
183
  $breadcrumb = array_merge(drupal_get_breadcrumb(), array_reverse($breadcrumb));
184
  drupal_set_breadcrumb($breadcrumb);
185
}
186

    
187
/**
188
 * Helper function to convert convert legacy vocabulary ids into machine names.
189
 *
190
 * @param array $vids
191
 *   Array of either vids.
192
 *
193
 * @return array
194
 *   A keyed array of machine names.
195
 */
196
function _ctools_term_vocabulary_machine_name_convert($vids) {
197
  $vocabularies = taxonomy_vocabulary_load_multiple($vids);
198
  $return = array();
199
  foreach ($vocabularies as $vocabulary) {
200
    $return[$vocabulary->machine_name] = $vocabulary->machine_name;
201
  }
202
  return $return;
203
}