Project

General

Profile

Paste
Download (3.68 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / views / handlers / views_handler_filter_entity_bundle.inc @ 8be7bf84

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
  /**
18
   * Stores the entity type on which the filter filters.
19
   *
20
   * @var string
21
   */
22
  public $entity_type;
23

    
24
  /**
25
   * {@inheritdoc}
26
   */
27
  public function init(&$view, &$options) {
28
    parent::init($view, $options);
29

    
30
    $this->get_entity_type();
31
  }
32

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

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

    
60
    return $this->entity_type;
61
  }
62

    
63
  /**
64
   * {@inheritdoc}
65
   */
66
  public function get_value_options() {
67
    if (!isset($this->value_options)) {
68
      $info = entity_get_info($this->entity_type);
69
      $types = $info['bundles'];
70
      $this->value_title = t('@entity types', array('@entity' => $info['label']));
71

    
72
      $options = array();
73
      foreach ($types as $type => $info) {
74
        $options[$type] = t($info['label']);
75
      }
76
      asort($options);
77
      $this->value_options = $options;
78
    }
79
  }
80

    
81
  /**
82
   * All entity types beside comment and taxonomy terms have a proper implement
83
   * bundle, though these two need an additional join to node/vocab table
84
   * to work as required.
85
   */
86
  public function query() {
87
    $this->ensure_my_table();
88

    
89
    // Adjust the join for the comment case.
90
    if ($this->entity_type == 'comment') {
91
      $join = new views_join();
92
      $def = array(
93
        'table' => 'node',
94
        'field' => 'nid',
95
        'left_table' => $this->table_alias,
96
        'left_field' => 'nid',
97
      );
98
      $join->definition = $def;
99
      $join->construct();
100
      $join->adjusted = TRUE;
101
      $this->table_alias = $this->query->add_table('node', $this->relationship, $join);
102
      $this->real_field = 'type';
103

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

    
130
}