You are here

public function SearchApiRange::execute in Facets 8

Adds facet info to the query using the backend native query object.

Overrides QueryTypeInterface::execute

File

src/Plugin/facets/query_type/SearchApiRange.php, line 24

Class

SearchApiRange
Provides support for range facets within the Search API scope.

Namespace

Drupal\facets\Plugin\facets\query_type

Code

public function execute() {
  $query = $this->query;

  // Only alter the query when there's an actual query object to alter.
  if (!empty($query)) {
    $operator = $this->facet
      ->getQueryOperator();
    $field_identifier = $this->facet
      ->getFieldIdentifier();
    $exclude = $this->facet
      ->getExclude();
    if ($query
      ->getProcessingLevel() === QueryInterface::PROCESSING_FULL) {

      // Set the options for the actual query.
      $options =& $query
        ->getOptions();
      $options['search_api_facets'][$field_identifier] = $this
        ->getFacetOptions();
    }

    // Add the filter to the query if there are active values.
    $active_items = $this->facet
      ->getActiveItems();
    if (count($active_items)) {
      $filter = $query
        ->createConditionGroup($operator, [
        'facet:' . $field_identifier,
      ]);
      foreach ($active_items as $value) {
        $filter
          ->addCondition($field_identifier, $value, $exclude ? 'NOT BETWEEN' : 'BETWEEN');
      }
      $query
        ->addConditionGroup($filter);
    }
  }
}