You are here

public function JsonApiFacets::fillFacetsWithResults in JSON:API Search API 8

Fills the facet entities with results from the facet source.

Parameters

\Drupal\facets\FacetInterface[] $facets: The configured facets.

Overrides FacetSourcePluginInterface::fillFacetsWithResults

File

modules/jsonapi_search_api_facets/src/Plugin/facets/facet_source/JsonApiFacets.php, line 84

Class

JsonApiFacets
Provides a facet source for use in JSON:API.

Namespace

Drupal\jsonapi_search_api_facets\Plugin\facets\facet_source

Code

public function fillFacetsWithResults(array $facets) {

  // Check if the results for this search ID are already populated in the
  // query helper.
  $results = $this->searchApiQueryHelper
    ->getResults(strtr('jsonapi_search_api:!index', [
    '!index' => $this->index
      ->id(),
  ]));
  if (!$results instanceof ResultSetInterface) {
    return;
  }

  // Get our facet data.
  $facet_results = $results
    ->getExtraData('search_api_facets');

  // If no data is found in the 'search_api_facets' extra data, we can stop
  // execution here.
  if ($facet_results === []) {
    return;
  }

  // Loop over each facet and execute the build method from the given query
  // type.
  foreach ($facets as $facet) {
    $configuration = [
      'query' => $results
        ->getQuery(),
      'facet' => $facet,
      'results' => $facet_results[$facet
        ->getFieldIdentifier()] ?? [],
    ];

    // Get the Facet Specific Query Type so we can process the results
    // using the build() function of the query type.
    $query_type = $this->queryTypePluginManager
      ->createInstance($facet
      ->getQueryType(), $configuration);
    $query_type
      ->build();
  }
}