Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views / modules / taxonomy / views_handler_relationship_node_term_data.inc @ 6eb8d15f

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_handler_relationship_node_term_data.
6
 */
7

    
8
/**
9
 * Relationship handler to return the taxonomy terms of nodes.
10
 *
11
 * @ingroup views_relationship_handlers
12
 */
13
class views_handler_relationship_node_term_data extends views_handler_relationship  {
14
  function init(&$view, &$options) {
15
    parent::init($view, $options);
16

    
17
    // Convert legacy vids option to machine name vocabularies.
18
    if (!empty($this->options['vids'])) {
19
      $vocabularies = taxonomy_get_vocabularies();
20
      foreach ($this->options['vids'] as $vid) {
21
        if (isset($vocabularies[$vid], $vocabularies[$vid]->machine_name)) {
22
          $this->options['vocabularies'][$vocabularies[$vid]->machine_name] = $vocabularies[$vid]->machine_name;
23
        }
24
      }
25
    }
26
  }
27

    
28
  function option_definition() {
29
    $options = parent::option_definition();
30
    $options['vocabularies'] = array('default' => array());
31
    return $options;
32
  }
33

    
34
  function options_form(&$form, &$form_state) {
35
    $vocabularies = taxonomy_get_vocabularies();
36
    $options = array();
37
    foreach ($vocabularies as $voc) {
38
      $options[$voc->machine_name] = check_plain($voc->name);
39
    }
40

    
41
    $form['vocabularies'] = array(
42
      '#type' => 'checkboxes',
43
      '#title' => t('Vocabularies'),
44
      '#options' => $options,
45
      '#default_value' => $this->options['vocabularies'],
46
      '#description' => t('Choose which vocabularies you wish to relate. Remember that every term found will create a new record, so this relationship is best used on just one vocabulary that has only one term per node.'),
47
    );
48
    parent::options_form($form, $form_state);
49
  }
50

    
51
  /**
52
   * Called to implement a relationship in a query.
53
   */
54
  function query() {
55
    $this->ensure_my_table();
56

    
57
    $def = $this->definition;
58
    $def['table'] = 'taxonomy_term_data';
59

    
60
    if (!array_filter($this->options['vocabularies'])) {
61
      $taxonomy_index = $this->query->add_table('taxonomy_index', $this->relationship);
62
      $def['left_table'] = $taxonomy_index;
63
      $def['left_field'] = 'tid';
64
      $def['field'] = 'tid';
65
      $def['type'] = empty($this->options['required']) ? 'LEFT' : 'INNER';
66
    }
67
    else {
68
      // If vocabularies are supplied join a subselect instead
69
      $def['left_table'] = $this->table_alias;
70
      $def['left_field'] = 'nid';
71
      $def['field'] = 'nid';
72
      $def['type'] = empty($this->options['required']) ? 'LEFT' : 'INNER';
73

    
74
      $query = db_select('taxonomy_term_data', 'td');
75
      $query->addJoin($def['type'], 'taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
76
      $query->addJoin($def['type'], 'taxonomy_index', 'tn', 'tn.tid = td.tid');
77
      $query->condition('tv.machine_name', array_filter($this->options['vocabularies']));
78
      if (empty($this->query->options['disable_sql_rewrite'])) {
79
        $query->addTag('term_access');
80
      }
81
      $query->fields('td');
82
      $query->fields('tn', array('nid'));
83
      $def['table formula'] = $query;
84
    }
85

    
86
    $join = new views_join();
87

    
88
    $join->definition = $def;
89
    $join->construct();
90
    $join->adjusted = TRUE;
91

    
92
    // use a short alias for this:
93
    $alias = $def['table'] . '_' . $this->table;
94

    
95
    $this->alias = $this->query->add_relationship($alias, $join, 'taxonomy_term_data', $this->relationship);
96
  }
97
}