You are here

public function CoreViewsContextualFilter::fillFacetsWithResults in Core Views Facets 8

Throws

\Drupal\Component\Plugin\Exception\PluginException

Overrides FacetSourcePluginInterface::fillFacetsWithResults

File

src/Plugin/facets/facet_source/CoreViewsContextualFilter.php, line 110

Class

CoreViewsContextualFilter
Represents a facet source of the core views with contextual 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;
      }
    }
    $views_arguments = $this
      ->getFields();
    reset($views_arguments);
    if (!empty($views_arguments[$facet
      ->getFieldIdentifier()])) {
      $facet_argument = $views_arguments[$facet
        ->getFieldIdentifier()];
    }
    else {
      return;
    }
    $request_arguments = [];
    $map = $this
      ->getViewsArgumentsMap();
    foreach ($map as $current_argument) {
      $request_arguments[] = $current_argument['active'] ? $current_argument['value'] : $current_argument['neutral_value'];
    }
    $this->view
      ->setArguments($request_arguments);
    $this->view
      ->build($this->pluginDefinition['view_display']);
    $facet_core_views_contextual_filter_plugin = $this
      ->loadFacetCoreViewsContextualFilterTypePlugin($facet_argument);
    if (empty($facet_core_views_contextual_filter_plugin)) {
      return;
    }
    $query = $facet_core_views_contextual_filter_plugin
      ->prepareQuery($this->view, $facet_argument, $facet);
    if (empty($query)) {
      return;
    }
    $rows = $query
      ->execute();
    $facet_results = [];
    while ($row = $rows
      ->fetchObject()) {
      $facet_results[] = $facet_core_views_contextual_filter_plugin
        ->processDatabaseRow($row, $facet_argument, $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();
  }
}