You are here

public function SearchApiSubscriber::queryAlter in Facets 8

Same name in this branch
  1. 8 src/EventSubscriber/SearchApiSubscriber.php \Drupal\facets\EventSubscriber\SearchApiSubscriber::queryAlter()
  2. 8 modules/facets_summary/src/EventSubscriber/SearchApiSubscriber.php \Drupal\facets_summary\EventSubscriber\SearchApiSubscriber::queryAlter()

Reacts to the query alter event.

Parameters

\Drupal\search_api\Event\QueryPreExecuteEvent $event: The query alter event.

File

modules/facets_summary/src/EventSubscriber/SearchApiSubscriber.php, line 33

Class

SearchApiSubscriber

Namespace

Drupal\facets_summary\EventSubscriber

Code

public function queryAlter(QueryPreExecuteEvent $event) {
  $query = $event
    ->getQuery();
  $facet_source_id = 'search_api:' . str_replace(':', '__', $query
    ->getSearchId());
  $storage = $this->entityTypeManager
    ->getStorage('facets_summary');

  // Get all the facet summaries for the facet source.
  $facet_summaries = $storage
    ->loadByProperties([
    'facet_source_id' => $facet_source_id,
  ]);

  /** @var \Drupal\facets_summary\FacetsSummaryInterface $facet_summary */
  foreach ($facet_summaries as $facet_summary) {
    $processors = $facet_summary
      ->getProcessors();

    // If the count processor is enabled, results count must not be skipped.
    if (in_array('show_count', array_keys($processors))) {
      $query
        ->setOption('skip result count', FALSE);
      break;
    }
  }
}