5 * Definition of views_handler_filter_term_node_tid_depth_join.
9 * Filter handler for taxonomy terms with depth.
11 * This handler is actually part of the node table and has some restrictions,
12 * because it uses a subquery to find nodes with.
14 * @ingroup views_filter_handlers
16 class views_handler_filter_term_node_tid_depth_join extends views_handler_filter_term_node_tid {
17 function operator_options($which = 'title') {
19 'or' => t('Is one of'),
23 function option_definition() {
24 $options = parent::option_definition();
26 $options['depth'] = array('default' => 0);
31 function extra_options_form(&$form, &$form_state) {
32 parent::extra_options_form($form, $form_state);
34 $form['depth'] = array(
36 '#title' => t('Depth'),
37 '#default_value' => $this->options['depth'],
38 '#description' => t('The depth will match nodes tagged with terms in the hierarchy. For example, if you have the term "fruit" and a child term "apple", with a depth of 1 (or higher) then filtering for the term "fruit" will get nodes that are tagged with "apple" as well as "fruit". If negative, the reverse is true; searching for "apple" will also pick up nodes tagged with "fruit" if depth is -1 (or lower).'),
43 // If no filter values are present, then do nothing.
44 if (count($this->value) == 0) {
47 elseif (count($this->value) == 1) {
48 // Somethis $this->value is an array with a single element so convert it.
49 if (is_array($this->value)) {
50 $this->value = current($this->value);
55 $operator = 'IN';# " IN (" . implode(', ', array_fill(0, sizeof($this->value), '%d')) . ")";
58 // The normal use of ensure_my_table() here breaks Views.
59 // So instead we trick the filter into using the alias of the base table.
60 // See http://drupal.org/node/271833
61 // If a relationship is set, we must use the alias it provides.
62 if (!empty($this->relationship)) {
63 $this->table_alias = $this->relationship;
65 // If no relationship, then use the alias of the base table.
66 elseif (isset($this->query->table_queue[$this->query->base_table]['alias'])) {
67 $this->table_alias = $this->query->table_queue[$this->query->base_table]['alias'];
69 // This should never happen, but if it does, we fail quietly.
74 // The tids variable can be an integer or an array of integers.
75 $tids = is_array($this->value) ? $this->value : array($this->value);
77 if ($this->options['depth'] > 0) {
78 // When the depth is positive search the children.
79 foreach ($tids as $tid) {
80 // The term must be loaded to get vid for use in taxonomy_get_tree().
81 if ($term = taxonomy_term_load($tid)) {
82 // For every tid argument find all the children down to the depth set
83 // in the options and save the tids for the condition.
84 $tree = taxonomy_get_tree($term->vid, $term->tid, (int) $this->options['depth']);
85 $tids = array_merge($tids, array_map('_taxonomy_get_tid_from_term', $tree));
89 elseif ($this->options['depth'] < 0) {
90 // When the depth is negative search the parents.
91 foreach ($tids as $tid) {
92 // For every tid argument find all the parents up to the depth set
93 // in the options and add the tids into the array. Since there is
94 // no taxonomy function to get all parents with a depth limit it
95 // is done here building a multidimensional array.
96 if ($term = taxonomy_term_load($tid)) {
97 // A variable is needed to track the current depth level.
99 // Initialise our depth based parents array with the leaf term.
100 $parents[$n--][] = $term;
101 while ($n >= $this->options['depth']) {
102 // At each depth find the parents of the current terms.
103 // It is important to note that it is possible for a term to have
104 // multiple parents so get the parents of every parent and so on.
105 $parents[$n] = array();
106 foreach ($parents[$n + 1] as $term) {
107 $parents[$n] += taxonomy_get_parents($term->tid);
109 // Save all the tids for the condition.
110 $tids = array_merge($tids, array_map('_taxonomy_get_tid_from_term', $parents[$n]));
117 // Check the size of the array and set the operator accordingly.
118 if (count($tids) > 1) {
122 $tids = current($tids);
126 // Join on taxonomy index table.
127 $join = new views_join();
128 $join->table = 'taxonomy_index';
129 $join->field = 'nid';
130 $join->left_table = $this->table_alias;
131 $join->left_field = $this->real_field;
132 $join->type = 'INNER';
133 $join->extra = array(
137 'operator' => $operator,
140 $taxonomy_index_alias = $this->query->add_relationship('taxonomy_index', $join, 'node');