Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views / handlers / views_handler_filter_entity_bundle.inc @ 651307cd

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_handler_filter_entity_bundle
6
 */
7

    
8
/**
9
 * Filter class which allows to filter by certain bundles of an entity.
10
 *
11
 * This class provides workarounds for taxonomy and comment.
12
 *
13
 * @ingroup views_filter_handlers
14
 */
15
class views_handler_filter_entity_bundle extends views_handler_filter_in_operator {
16
  /**
17
   * Stores the entity type on which the filter filters.
18
   *
19
   * @var string
20
   */
21
  public $entity_type;
22

    
23
  function init(&$view, &$options) {
24
    parent::init($view, $options);
25

    
26
    $this->get_entity_type();
27
  }
28

    
29
  /**
30
   * Set and returns the entity_type.
31
   *
32
   * @return string
33
   *   The entity type on the filter.
34
   */
35
  function get_entity_type() {
36
    if (!isset($this->entity_type)) {
37
      $data = views_fetch_data($this->table);
38
      if (isset($data['table']['entity type'])) {
39
        $this->entity_type = $data['table']['entity type'];
40
      }
41

    
42
      // If the current filter is under a relationship you can't be sure that the
43
      // entity type of the view is the entity type of the current filter
44
      // For example a filter from a node author on a node view does have users as entity type.
45
      if (!empty($this->options['relationship']) && $this->options['relationship'] != 'none') {
46
        $relationships = $this->view->display_handler->get_option('relationships');
47
        if (!empty($relationships[$this->options['relationship']])) {
48
          $options = $relationships[$this->options['relationship']];
49
          $data = views_fetch_data($options['table']);
50
          $this->entity_type = $data['table']['entity type'];
51
        }
52
      }
53
    }
54

    
55
    return $this->entity_type;
56
  }
57

    
58

    
59
  function get_value_options() {
60
    if (!isset($this->value_options)) {
61
      $info = entity_get_info($this->entity_type);
62
      $types = $info['bundles'];
63
      $this->value_title = t('@entity types', array('@entity' => $info['label']));
64

    
65
      $options = array();
66
      foreach ($types as $type => $info) {
67
        $options[$type] = t($info['label']);
68
      }
69
      asort($options);
70
      $this->value_options = $options;
71
    }
72
  }
73

    
74
  /**
75
   * All entity types beside comment and taxonomy terms have a proper implement
76
   * bundle, though these two need an additional join to node/vocab table
77
   * to work as required.
78
   */
79
  function query() {
80
    $this->ensure_my_table();
81

    
82
    // Adjust the join for the comment case.
83
    if ($this->entity_type == 'comment') {
84
      $join = new views_join();
85
      $def = array(
86
        'table' => 'node',
87
        'field' => 'nid',
88
        'left_table' => $this->table_alias,
89
        'left_field' => 'nid',
90
      );
91
      $join->definition = $def;
92
      $join->construct();
93
      $join->adjusted = TRUE;
94
      $this->table_alias = $this->query->add_table('node', $this->relationship, $join);
95
      $this->real_field = 'type';
96

    
97
      // Replace the value to match the node type column.
98
      foreach ($this->value as &$value) {
99
        $value = str_replace('comment_node_', '', $value);
100
      }
101
    }
102
    elseif ($this->entity_type == 'taxonomy_term') {
103
      $join = new views_join();
104
      $def = array(
105
        'table' => 'taxonomy_vocabulary',
106
        'field' => 'vid',
107
        'left_table' => $this->table_alias,
108
        'left_field' => 'vid',
109
      );
110
      $join->definition = $def;
111
      $join->construct();
112
      $join->adjusted = TRUE;
113
      $this->table_alias = $this->query->add_table('taxonomy_vocabulary', $this->relationship, $join);
114
      $this->real_field = 'machine_name';
115
    }
116
    else {
117
      $entity_info = entity_get_info($this->entity_type);
118
      $this->real_field = $entity_info['bundle keys']['bundle'];
119
    }
120
    parent::query();
121
  }
122
}