You are here

public function SearchApiViewsHandlerArgumentTaxonomyTerm::query in Search API 7

Set up the query for this argument.

The argument sent may be found at $this->argument.

Overrides SearchApiViewsHandlerArgument::query

File

contrib/search_api_views/includes/handler_argument_taxonomy_term.inc, line 18
Contains the SearchApiViewsHandlerArgumentTaxonomyTerm class.

Class

SearchApiViewsHandlerArgumentTaxonomyTerm
Defines a contextual filter searching through all indexed taxonomy fields.

Code

public function query($group_by = FALSE) {
  if (empty($this->value)) {
    $this
      ->fillValue();
  }
  $outer_conjunction = strtoupper($this->operator);
  if (empty($this->options['not'])) {
    $operator = '=';
    $inner_conjunction = 'OR';
  }
  else {
    $operator = '<>';
    $inner_conjunction = 'AND';
  }
  if (!empty($this->value)) {
    $terms = entity_load('taxonomy_term', $this->value);
    if (!empty($terms)) {
      $filter = $this->query
        ->createFilter($outer_conjunction);
      $vocabulary_fields = $this->definition['vocabulary_fields'];
      $vocabulary_fields += array(
        '' => array(),
      );
      foreach ($terms as $term) {
        $inner_filter = $filter;
        if ($outer_conjunction != $inner_conjunction) {
          $inner_filter = $this->query
            ->createFilter($inner_conjunction);
        }

        // Set filters for all term reference fields which don't specify a
        // vocabulary, as well as for all fields specifying the term's
        // vocabulary.
        if (!empty($this->definition['vocabulary_fields'][$term->vocabulary_machine_name])) {
          foreach ($this->definition['vocabulary_fields'][$term->vocabulary_machine_name] as $field) {
            $inner_filter
              ->condition($field, $term->tid, $operator);
          }
        }
        foreach ($vocabulary_fields[''] as $field) {
          $inner_filter
            ->condition($field, $term->tid, $operator);
        }
        if ($outer_conjunction != $inner_conjunction) {
          $filter
            ->filter($inner_filter);
        }
      }
      $this->query
        ->filter($filter);
    }
  }
}