Projet

Général

Profil

Paste
Télécharger (5,07 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / modules / taxonomy / views_handler_field_term_node_tid.inc @ 7547bb19

1
<?php
2

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

    
8
/**
9
 * Field handler to display all taxonomy terms of a node.
10
 *
11
 * @ingroup views_field_handlers
12
 */
13
class views_handler_field_term_node_tid extends views_handler_field_prerender_list {
14
  function init(&$view, &$options) {
15
    parent::init($view, $options);
16
    // @todo: Wouldn't it be possible to use $this->base_table and no if here?
17
    if ($view->base_table == 'node_revision') {
18
      $this->additional_fields['nid'] = array('table' => 'node_revision', 'field' => 'nid');
19
    }
20
    else {
21
      $this->additional_fields['nid'] = array('table' => 'node', 'field' => 'nid');
22
    }
23

    
24
    // Convert legacy vids option to machine name vocabularies.
25
    if (!empty($this->options['vids'])) {
26
      $vocabularies = taxonomy_get_vocabularies();
27
      foreach ($this->options['vids'] as $vid) {
28
        if (isset($vocabularies[$vid], $vocabularies[$vid]->machine_name)) {
29
          $this->options['vocabularies'][$vocabularies[$vid]->machine_name] = $vocabularies[$vid]->machine_name;
30
        }
31
      }
32
    }
33
  }
34

    
35
  function option_definition() {
36
    $options = parent::option_definition();
37

    
38
    $options['link_to_taxonomy'] = array('default' => TRUE, 'bool' => TRUE);
39
    $options['limit'] = array('default' => FALSE, 'bool' => TRUE);
40
    $options['vocabularies'] = array('default' => array());
41

    
42
    return $options;
43
  }
44

    
45
  /**
46
   * Provide "link to term" option.
47
   */
48
  function options_form(&$form, &$form_state) {
49
    $form['link_to_taxonomy'] = array(
50
      '#title' => t('Link this field to its term page'),
51
      '#type' => 'checkbox',
52
      '#default_value' => !empty($this->options['link_to_taxonomy']),
53
    );
54

    
55
    $form['limit'] = array(
56
      '#type' => 'checkbox',
57
      '#title' => t('Limit terms by vocabulary'),
58
      '#default_value'=> $this->options['limit'],
59
    );
60

    
61
    $options = array();
62
    $vocabularies = taxonomy_get_vocabularies();
63
    foreach ($vocabularies as $voc) {
64
      $options[$voc->machine_name] = check_plain($voc->name);
65
    }
66

    
67
    $form['vocabularies'] = array(
68
      '#prefix' => '<div><div id="edit-options-vocabularies">',
69
      '#suffix' => '</div></div>',
70
      '#type' => 'checkboxes',
71
      '#title' => t('Vocabularies'),
72
      '#options' => $options,
73
      '#default_value' => $this->options['vocabularies'],
74
      '#dependency' => array('edit-options-limit' => array(TRUE)),
75
    );
76

    
77
    parent::options_form($form, $form_state);
78
  }
79

    
80
  /**
81
   * Add this term to the query
82
   */
83
  function query() {
84
    $this->add_additional_fields();
85
  }
86

    
87
  function pre_render(&$values) {
88
    $this->field_alias = $this->aliases['nid'];
89
    $nids = array();
90
    foreach ($values as $result) {
91
      if (!empty($result->{$this->aliases['nid']})) {
92
        $nids[] = $result->{$this->aliases['nid']};
93
      }
94
    }
95

    
96
    if ($nids) {
97
      $query = db_select('taxonomy_term_data', 'td');
98
      $query->innerJoin('taxonomy_index', 'tn', 'td.tid = tn.tid');
99
      $query->innerJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
100
      $query->fields('td');
101
      $query->addField('tn', 'nid', 'node_nid');
102
      $query->addField('tv', 'name', 'vocabulary');
103
      $query->addField('tv', 'machine_name', 'vocabulary_machine_name');
104
      $query->orderby('td.weight');
105
      $query->orderby('td.name');
106
      $query->condition('tn.nid', $nids);
107
      $query->addTag('term_access');
108
      $vocabs = array_filter($this->options['vocabularies']);
109
      if (!empty($this->options['limit']) && !empty($vocabs)) {
110
        $query->condition('tv.machine_name', $vocabs);
111
      }
112
      $result = $query->execute();
113

    
114
      foreach ($result as $term) {
115
        $this->items[$term->node_nid][$term->tid]['name'] = check_plain($term->name);
116
        $this->items[$term->node_nid][$term->tid]['tid'] = $term->tid;
117
        $this->items[$term->node_nid][$term->tid]['vocabulary_machine_name'] = check_plain($term->vocabulary_machine_name);
118
        $this->items[$term->node_nid][$term->tid]['vocabulary'] = check_plain($term->vocabulary);
119

    
120
        if (!empty($this->options['link_to_taxonomy'])) {
121
          $this->items[$term->node_nid][$term->tid]['make_link'] = TRUE;
122
          $this->items[$term->node_nid][$term->tid]['path'] = 'taxonomy/term/' . $term->tid;
123
        }
124
      }
125
    }
126
  }
127

    
128
  function render_item($count, $item) {
129
    return $item['name'];
130
  }
131

    
132
  function document_self_tokens(&$tokens) {
133
    $tokens['[' . $this->options['id'] . '-tid' . ']'] = t('The taxonomy term ID for the term.');
134
    $tokens['[' . $this->options['id'] . '-name' . ']'] = t('The taxonomy term name for the term.');
135
    $tokens['[' . $this->options['id'] . '-vocabulary-machine-name' . ']'] = t('The machine name for the vocabulary the term belongs to.');
136
    $tokens['[' . $this->options['id'] . '-vocabulary' . ']'] = t('The name for the vocabulary the term belongs to.');
137
  }
138

    
139
  function add_self_tokens(&$tokens, $item) {
140
    foreach(array('tid', 'name', 'vocabulary_machine_name', 'vocabulary') as $token) {
141
      // Replace _ with - for the vocabulary machine name.
142
      $tokens['[' . $this->options['id'] . '-' . str_replace('_', '-', $token). ']'] = isset($item[$token]) ? $item[$token] : '';
143
    }
144
  }
145
}