views_handler_filter_term_node_tid_depth.inc

  1. 3.x modules/taxonomy/views_handler_filter_term_node_tid_depth.inc
  2. 2.x modules/taxonomy/views_handler_filter_term_node_tid_depth.inc

Definition of views_handler_filter_term_node_tid_depth.

File

modules/taxonomy/views_handler_filter_term_node_tid_depth.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_filter_term_node_tid_depth.
  5. */
  6. /**
  7. * Filter handler for taxonomy terms with depth.
  8. *
  9. * This handler is actually part of the node table and has some restrictions,
  10. * because it uses a subquery to find nodes with.
  11. *
  12. * @ingroup views_filter_handlers
  13. */
  14. class views_handler_filter_term_node_tid_depth extends views_handler_filter_term_node_tid {
  15. function operator_options($which = 'title') {
  16. return array(
  17. 'or' => t('Is one of'),
  18. );
  19. }
  20. function option_definition() {
  21. $options = parent::option_definition();
  22. $options['depth'] = array('default' => 0);
  23. return $options;
  24. }
  25. function extra_options_form(&$form, &$form_state) {
  26. parent::extra_options_form($form, $form_state);
  27. $form['depth'] = array(
  28. '#type' => 'weight',
  29. '#title' => t('Depth'),
  30. '#default_value' => $this->options['depth'],
  31. '#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).'),
  32. );
  33. }
  34. function query() {
  35. // If no filter values are present, then do nothing.
  36. if (count($this->value) == 0) {
  37. return;
  38. }
  39. elseif (count($this->value) == 1) {
  40. // Somethis $this->value is an array with a single element so convert it.
  41. if (is_array($this->value)) {
  42. $this->value = current($this->value);
  43. }
  44. $operator = '=';
  45. }
  46. else {
  47. $operator = 'IN';# " IN (" . implode(', ', array_fill(0, sizeof($this->value), '%d')) . ")";
  48. }
  49. // The normal use of ensure_my_table() here breaks Views.
  50. // So instead we trick the filter into using the alias of the base table.
  51. // See http://drupal.org/node/271833
  52. // If a relationship is set, we must use the alias it provides.
  53. if (!empty($this->relationship)) {
  54. $this->table_alias = $this->relationship;
  55. }
  56. // If no relationship, then use the alias of the base table.
  57. elseif (isset($this->query->table_queue[$this->query->base_table]['alias'])) {
  58. $this->table_alias = $this->query->table_queue[$this->query->base_table]['alias'];
  59. }
  60. // This should never happen, but if it does, we fail quietly.
  61. else {
  62. return;
  63. }
  64. // Now build the subqueries.
  65. $subquery = db_select('taxonomy_index', 'tn');
  66. $subquery->addField('tn', 'nid');
  67. $where = db_or()->condition('tn.tid', $this->value, $operator);
  68. $last = "tn";
  69. if ($this->options['depth'] > 0) {
  70. $subquery->leftJoin('taxonomy_term_hierarchy', 'th', "th.tid = tn.tid");
  71. $last = "th";
  72. foreach (range(1, abs($this->options['depth'])) as $count) {
  73. $subquery->leftJoin('taxonomy_term_hierarchy', "th$count", "$last.parent = th$count.tid");
  74. $where->condition("th$count.tid", $this->value, $operator);
  75. $last = "th$count";
  76. }
  77. }
  78. elseif ($this->options['depth'] < 0) {
  79. foreach (range(1, abs($this->options['depth'])) as $count) {
  80. $subquery->leftJoin('taxonomy_term_hierarchy', "th$count", "$last.tid = th$count.parent");
  81. $where->condition("th$count.tid", $this->value, $operator);
  82. $last = "th$count";
  83. }
  84. }
  85. $subquery->condition($where);
  86. $this->query->add_where($this->options['group'], "$this->table_alias.$this->real_field", $subquery, 'IN');
  87. }
  88. }