You are here

public function CoreViewsExposedFilter::fillFacetsWithResults in Core Views Facets 8

Throws

\Drupal\Component\Plugin\Exception\PluginException

Overrides FacetSourcePluginInterface::fillFacetsWithResults

File

src/Plugin/facets/facet_source/CoreViewsExposedFilter.php, line 111

Class

CoreViewsExposedFilter
Represents a facet source of the core views with exposed filters.

Namespace

Drupal\core_views_facets\Plugin\facets\facet_source

Code

public function fillFacetsWithResults(array $facets) {
  foreach ($facets as $facet) {
    if ($facet
      ->getOnlyVisibleWhenFacetSourceIsVisible()) {

      // Ignore currently unnecessary facets.

      /** @var \Drupal\facets\FacetSource\FacetSourcePluginInterface $facet_source */
      $facet_source = $facet
        ->getFacetSource();
      if (!$facet_source
        ->isRenderedInCurrentRequest()) {
        continue;
      }
    }
    $request_arguments = [];
    $map = $this
      ->getViewsArgumentsMap();
    foreach ($map as $current_argument) {
      if ($current_argument['active']) {
        $request_arguments[] = $current_argument['value'];
      }
      else {
        break;
      }
    }
    $this->view
      ->setArguments($request_arguments);
    $filters = $this
      ->getFields();
    if (empty($filters[$facet
      ->getFieldIdentifier()])) {
      return;
    }
    $filter = $filters[$facet
      ->getFieldIdentifier()];
    $this->view
      ->build($this->pluginDefinition['view_display']);
    $facet_core_views_exposed_filter_plugin = $this
      ->loadFacetCoreViewsExposedFilterTypePlugin($filter);
    if (empty($facet_core_views_exposed_filter_plugin)) {
      return;
    }
    $query = $facet_core_views_exposed_filter_plugin
      ->prepareQuery($this->view, $filter, $facet);
    if (empty($query)) {
      return;
    }
    try {
      $rows = $query
        ->execute();
    } catch (DatabaseExceptionWrapper $e) {
      continue;
    }
    $facet_results = [];
    while ($row = $rows
      ->fetchObject()) {
      $facet_results[] = $facet_core_views_exposed_filter_plugin
        ->processDatabaseRow($row, $filter, $facet);
    }
    $configuration = [
      'query' => NULL,
      'facet' => $facet,
    ];
    $facet
      ->setResults($facet_results);

    // 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();
  }
}