You are here

public function SearchApiString::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/SearchApiString.php, line 29

Class

SearchApiString
Provides support for string 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($this->facet
          ->getFieldIdentifier(), $value, $exclude ? '<>' : '=');
      }
      $query
        ->addConditionGroup($filter);
    }
  }
}