handler_argument_taxonomy_term.inc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * @file
  4. * Contains the SearchApiViewsHandlerArgumentTaxonomyTerm class.
  5. */
  6. /**
  7. * Defines a contextual filter searching through all indexed taxonomy fields.
  8. */
  9. class SearchApiViewsHandlerArgumentTaxonomyTerm extends SearchApiViewsHandlerArgument {
  10. /**
  11. * Set up the query for this argument.
  12. *
  13. * The argument sent may be found at $this->argument.
  14. */
  15. public function query($group_by = FALSE) {
  16. if (empty($this->value)) {
  17. $this->fillValue();
  18. }
  19. $outer_conjunction = strtoupper($this->operator);
  20. if (empty($this->options['not'])) {
  21. $operator = '=';
  22. $inner_conjunction = 'OR';
  23. }
  24. else {
  25. $operator = '<>';
  26. $inner_conjunction = 'AND';
  27. }
  28. if (!empty($this->value)) {
  29. $terms = entity_load('taxonomy_term', $this->value);
  30. if (!empty($terms)) {
  31. $filter = $this->query->createFilter($outer_conjunction);
  32. $vocabulary_fields = $this->definition['vocabulary_fields'];
  33. $vocabulary_fields += array('' => array());
  34. foreach ($terms as $term) {
  35. $inner_filter = $filter;
  36. if ($outer_conjunction != $inner_conjunction) {
  37. $inner_filter = $this->query->createFilter($inner_conjunction);
  38. }
  39. // Set filters for all term reference fields which don't specify a
  40. // vocabulary, as well as for all fields specifying the term's
  41. // vocabulary.
  42. if (!empty($this->definition['vocabulary_fields'][$term->vocabulary_machine_name])) {
  43. foreach ($this->definition['vocabulary_fields'][$term->vocabulary_machine_name] as $field) {
  44. $inner_filter->condition($field, $term->tid, $operator);
  45. }
  46. }
  47. foreach ($vocabulary_fields[''] as $field) {
  48. $inner_filter->condition($field, $term->tid, $operator);
  49. }
  50. if ($outer_conjunction != $inner_conjunction) {
  51. $filter->filter($inner_filter);
  52. }
  53. }
  54. $this->query->filter($filter);
  55. }
  56. }
  57. }
  58. /**
  59. * Get the title this argument will assign the view, given the argument.
  60. */
  61. public function title() {
  62. if (!empty($this->argument)) {
  63. if (empty($this->value)) {
  64. $this->fillValue();
  65. }
  66. $terms = array();
  67. foreach ($this->value as $tid) {
  68. $taxonomy_term = taxonomy_term_load($tid);
  69. if ($taxonomy_term) {
  70. $terms[] = check_plain($taxonomy_term->name);
  71. }
  72. }
  73. return $terms ? implode(', ', $terms) : check_plain($this->argument);
  74. }
  75. else {
  76. return check_plain($this->argument);
  77. }
  78. }
  79. /**
  80. * Fill $this->value with data from the argument.
  81. *
  82. * Uses views_break_phrase(), if appropriate.
  83. */
  84. protected function fillValue() {
  85. if (!empty($this->options['break_phrase'])) {
  86. views_break_phrase($this->argument, $this);
  87. }
  88. else {
  89. $this->value = array($this->argument);
  90. }
  91. }
  92. }