You are here

public function DefaultFacetManager::processFacets in Facets 8

Initializes facet builds, sets the breadcrumb trail.

Facets are built via FacetsFacetProcessor objects. Facets only need to be processed, or built, once The FacetsFacetManager::processed semaphore is set when this method is called ensuring that facets are built only once regardless of how many times this method is called.

Parameters

string|null $facetsource_id: The facetsource if of the currently processed facet.

Throws

\Drupal\facets\Exception\InvalidProcessorException Thrown when one of the defined processors is invalid.

2 calls to DefaultFacetManager::processFacets()
DefaultFacetManager::build in src/FacetManager/DefaultFacetManager.php
Builds a facet and returns it as a renderable array.
DefaultFacetManager::returnProcessedFacet in src/FacetManager/DefaultFacetManager.php
Returns one of the processed facets.

File

src/FacetManager/DefaultFacetManager.php, line 165

Class

DefaultFacetManager
The facet manager.

Namespace

Drupal\facets\FacetManager

Code

public function processFacets($facetsource_id = NULL) {
  if ($facetsource_id === NULL) {
    foreach ($this->facets as $facet) {
      $current_facetsource_id = $facet
        ->getFacetSourceId();
      $this
        ->processFacets($current_facetsource_id);
    }
  }
  $unprocessedFacets = array_filter($this->facets, function ($item) use ($facetsource_id) {

    /* @var \Drupal\facets\FacetInterface $item */
    return !isset($this->processedFacets[$facetsource_id][$item
      ->id()]);
  });

  // All facets were already processed on a previous run, so no need to do so
  // again.
  if (count($unprocessedFacets) === 0) {
    return;
  }
  $this
    ->updateResults($facetsource_id);
  foreach ($unprocessedFacets as $facet) {
    $processor_configs = $facet
      ->getProcessorConfigs();
    foreach ($facet
      ->getProcessorsByStage(ProcessorInterface::STAGE_POST_QUERY) as $processor) {
      $processor_config = $processor_configs[$processor
        ->getPluginDefinition()['id']]['settings'];
      $processor_config['facet'] = $facet;

      /** @var \Drupal\facets\processor\PostQueryProcessorInterface $post_query_processor */
      $post_query_processor = $this->processorPluginManager
        ->createInstance($processor
        ->getPluginDefinition()['id'], $processor_config);
      if (!$post_query_processor instanceof PostQueryProcessorInterface) {
        throw new InvalidProcessorException("The processor {$processor->getPluginDefinition()['id']} has a post_query definition but doesn't implement the required PostQueryProcessor interface");
      }
      $post_query_processor
        ->postQuery($facet);
    }
    $this->processedFacets[$facetsource_id][$facet
      ->id()] = $facet;
  }
}