Projet

Général

Profil

Paste
Télécharger (3,11 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / plugins / contexts / terms.inc @ 6e3ce7c2

1
<?php
2

    
3
/**
4
 * @file
5
 * Plugin to provide a terms 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 terms"),
14
  'description' => t('Multiple taxonomy terms, as a group.'),
15
  'context' => 'ctools_context_create_terms',
16
  'keyword' => 'terms',
17
  // This context is deprecated and should not be usable in the UI.
18
  'no ui' => TRUE,
19
  'context name' => 'terms',
20
  'convert list' => array(
21
    'tid' => t('Term ID of first term'),
22
    'tids' => t('Term ID of all term, separated by + or ,'),
23
    'name' => t('Term name of first term'),
24
    'name_dashed' => t('Term name of first term, lowercased and spaces converted to dashes'),
25
    'names' => t('Term name of all terms, separated by + or ,'),
26
    'names_dashed' => t('Term name of all terms, separated by + or , and lowercased and spaces converted to dashes'),
27
    'vid' => t('Vocabulary ID of first term'),
28
  ),
29
  'convert' => 'ctools_context_terms_convert',
30
);
31

    
32
/**
33
 * It's important to remember that $conf is optional here, because contexts
34
 * are not always created from the UI.
35
 */
36
function ctools_context_create_terms($empty, $data = NULL, $conf = FALSE) {
37
  // The input is expected to be an object as created by ctools_break_phrase
38
  // which contains a group of terms.
39
  $context = new ctools_context(array('terms', 'entity:taxonomy_term'));
40
  $context->plugin = 'terms';
41

    
42
  if ($empty) {
43
    return $context;
44
  }
45

    
46
  if (!empty($data) && is_object($data)) {
47
    $context->operator = $data->operator;
48
    $context->tids     = $data->value;
49
    if (!isset($data->term)) {
50
      // Load the first term:
51
      reset($context->tids);
52
      $data->term = taxonomy_term_load(current($context->tids));
53
    }
54
    $context->data     = $data->term;
55
    $context->title    = $data->term->name;
56
    $context->argument = implode($context->operator == 'or' ? '+' : ',', array_unique($context->tids));
57
    return $context;
58
  }
59
}
60

    
61
/**
62
 * Convert a context into a string.
63
 */
64
function ctools_context_terms_convert($context, $type) {
65
  switch ($type) {
66
    case 'tid':
67
      return $context->data->tid;
68

    
69
    case 'tids':
70
      return $context->argument;
71

    
72
    case 'name':
73
      return $context->data->name;
74

    
75
    case 'name_dashed':
76
      return drupal_strtolower(str_replace(' ', '-', $context->data->name));
77

    
78
    case 'names':
79
    case 'names_dashed':
80
      // We only run this query if this item was requested:
81
      if (!isset($context->names)) {
82
        if (empty($context->tids)) {
83
          $context->names = '';
84
        }
85
        else {
86
          $result = db_query('SELECT tid, name FROM {taxonomy_term_data} WHERE tid IN (:tids)', array(':tids' => $context->tids));
87
          foreach ($result as $term) {
88
            $names[$term->tid] = $term->name;
89
            if ($type == 'names_dashed') {
90
              $names[$term->tid] = drupal_strtolower(str_replace(' ', '-', $names[$term->tid]));
91
            }
92
          }
93
          $context->names = implode($context->operator == 'or' ? ' + ' : ', ', $names);
94
        }
95
      }
96
      return $context->names;
97

    
98
    case 'vid':
99
      return $context->data->vid;
100
  }
101
}