You are here

public function DefaultFacetsSummaryManager::build in Facets 8

Builds a facet and returns it as a renderable array.

This method delegates to the relevant plugins to render a facet, it calls out to a widget plugin to do the actual rendering when results are found. When no results are found it calls out to the correct empty result plugin to build a render array.

Before doing any rendering, the processors that implement the BuildProcessorInterface enabled on this facet will run.

Parameters

\Drupal\facets_summary\FacetsSummaryInterface $facets_summary: The facet we should build.

Return value

array Facet render arrays.

Throws

\Drupal\facets\Exception\InvalidProcessorException Throws an exception when an invalid processor is linked to the facet.

File

modules/facets_summary/src/FacetsSummaryManager/DefaultFacetsSummaryManager.php, line 84

Class

DefaultFacetsSummaryManager
The facet summary manager.

Namespace

Drupal\facets_summary\FacetsSummaryManager

Code

public function build(FacetsSummaryInterface $facets_summary) {

  // Let the facet_manager build the facets.
  $facetsource_id = $facets_summary
    ->getFacetSourceId();

  /** @var \Drupal\facets\Entity\Facet[] $facets */
  $facets = $this->facetManager
    ->getFacetsByFacetSourceId($facetsource_id);

  // Get the current results from the facets and let all processors that
  // trigger on the build step do their build processing.
  // @see \Drupal\facets\Processor\BuildProcessorInterface.
  // @see \Drupal\facets\Processor\SortProcessorInterface.
  $this->facetManager
    ->updateResults($facetsource_id);
  $facets_config = $facets_summary
    ->getFacets();

  // Exclude facets which were not selected for this summary.
  $facets = array_filter($facets, function ($item) use ($facets_config) {
    return isset($facets_config[$item
      ->id()]);
  });
  foreach ($facets as $facet) {

    // Do not build the facet in summary if facet is not rendered.
    if (!$facet
      ->getActiveItems()) {
      continue;
    }

    // For clarity, process facets is called each build.
    // The first facet therefor will trigger the processing. Note that
    // processing is done only once, so repeatedly calling this method will
    // not trigger the processing more than once.
    $this->facetManager
      ->build($facet);
  }
  $build = [
    '#theme' => 'facets_summary_item_list',
    '#facet_summary_id' => $facets_summary
      ->id(),
    '#attributes' => [
      'data-drupal-facets-summary-id' => $facets_summary
        ->id(),
    ],
  ];
  $results = [];
  foreach ($facets as $facet) {
    $show_count = $facets_config[$facet
      ->id()]['show_count'];
    $results = array_merge($results, $this
      ->buildResultTree($show_count, $facet
      ->getResults()));
  }
  $build['#items'] = $results;

  // Allow our Facets Summary processors to alter the build array in a
  // configured order.
  foreach ($facets_summary
    ->getProcessorsByStage(ProcessorInterface::STAGE_BUILD) as $processor) {
    if (!$processor instanceof BuildProcessorInterface) {
      throw new InvalidProcessorException("The processor {$processor->getPluginDefinition()['id']} has a build definition but doesn't implement the required BuildProcessorInterface interface");
    }
    $build = $processor
      ->build($facets_summary, $build, $facets);
  }
  return $build;
}