SearchApiString.php 2.65 KB
Newer Older
1 2
<?php

3
namespace Drupal\facets\Plugin\facets\query_type;
4

5 6
use Drupal\facets\QueryType\QueryTypePluginBase;
use Drupal\facets\Result\Result;
7
use Drupal\search_api\Query\QueryInterface;
8 9 10 11 12 13 14 15 16 17 18

/**
 * Provides support for string facets within the Search API scope.
 *
 * This is the default implementation that works with all backends and data
 * types. While you could use this query type for every data type, other query
 * types will usually be better suited for their specific data type.
 *
 * For example, the SearchApiDate query type will handle its input as a DateTime
 * value, while this class would only be able to work with it as a string.
 *
19
 * @FacetsQueryType(
20 21 22 23 24 25 26 27 28 29 30 31
 *   id = "search_api_string",
 *   label = @Translation("String"),
 * )
 */
class SearchApiString extends QueryTypePluginBase {

  /**
   * {@inheritdoc}
   */
  public function execute() {
    $query = $this->query;

32 33 34
    // Only alter the query when there's an actual query object to alter.
    if (!empty($query)) {
      $operator = $this->facet->getQueryOperator();
35
      $field_identifier = $this->facet->getFieldIdentifier();
36
      $exclude = $this->facet->getExclude();
37

38 39 40 41 42
      if ($query->getProcessingLevel() === QueryInterface::PROCESSING_FULL) {
        // Set the options for the actual query.
        $options = &$query->getOptions();
        $options['search_api_facets'][$field_identifier] = $this->getFacetOptions();
      }
43 44 45

      // Add the filter to the query if there are active values.
      $active_items = $this->facet->getActiveItems();
46

47
      if (count($active_items)) {
48
        $filter = $query->createConditionGroup($operator, ['facet:' . $field_identifier]);
49
        foreach ($active_items as $value) {
50
          $filter->addCondition($this->facet->getFieldIdentifier(), $value, $exclude ? '<>' : '=');
51
        }
52
        $query->addConditionGroup($filter);
53 54 55 56 57 58 59 60
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
61 62
    $query_operator = $this->facet->getQueryOperator();

63
    if (!empty($this->results)) {
64
      $facet_results = [];
65
      foreach ($this->results as $result) {
66
        if ($result['count'] || $query_operator == 'or') {
67
          $result_filter = $result['filter'];
borisson_'s avatar
borisson_ committed
68
          if ($result_filter[0] === '"') {
69 70 71 72 73
            $result_filter = substr($result_filter, 1);
          }
          if ($result_filter[strlen($result_filter) - 1] === '"') {
            $result_filter = substr($result_filter, 0, -1);
          }
74
          $count = $result['count'];
75
          $result = new Result($this->facet, $result_filter, $result_filter, $count);
76
          $facet_results[] = $result;
77 78 79 80
        }
      }
      $this->facet->setResults($facet_results);
    }
81

82 83 84 85
    return $this->facet;
  }

}