/**
  * 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.
  *
  * @param \Drupal\facets\FacetInterface $facet
  *   The facet we should build.
  *
  * @return array
  *   Facet render arrays.
  *
  * @throws \Drupal\facets\Exception\InvalidProcessorException
  *   Throws an exception when an invalid processor is linked to the facet.
  */
 public function build(FacetInterface $facet)
 {
     // It might be that the facet received here, is not the same as the already
     // loaded facets in the FacetManager.
     // For that reason, get the facet from the already loaded facets in the
     // FacetManager.
     $facet = $this->facets[$facet->id()];
     $facet_source_id = $facet->getFacetSourceId();
     if ($facet->getOnlyVisibleWhenFacetSourceIsVisible()) {
         // Block rendering and processing should be stopped when the facet source
         // is not available on the page. Returning an empty array here is enough
         // to halt all further processing.
         $facet_source = $facet->getFacetSource();
         if (!$facet_source->isRenderedInCurrentRequest()) {
             return [];
         }
     }
     // 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->processFacets($facet_source_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\WidgetOrderProcessorInterface.
     $results = $facet->getResults();
     $active_sorts = [];
     // Load all processors, because getProcessorsByStage does not return the
     // correct configuration for the processors.
     // @todo: Fix when https://www.drupal.org/node/2722267 is fixed.
     $processors = $facet->getProcessors();
     foreach ($facet->getProcessorsByStage(ProcessorInterface::STAGE_BUILD) as $processor) {
         /** @var \Drupal\facets\Processor\BuildProcessorInterface $build_processor */
         $build_processor = $this->processorPluginManager->createInstance($processor->getPluginDefinition()['id'], ['facet' => $facet]);
         if ($build_processor instanceof WidgetOrderProcessorInterface) {
             // Sorting is handled last and together, to support nested sorts.
             $active_sorts[] = $processors[$build_processor->getPluginId()];
         } else {
             if (!$build_processor instanceof BuildProcessorInterface) {
                 throw new InvalidProcessorException("The processor {$processor->getPluginDefinition()['id']} has a build definition but doesn't implement the required BuildProcessorInterface interface");
             }
             $results = $build_processor->build($facet, $results);
         }
     }
     uasort($results, function ($a, $b) use($active_sorts) {
         $return = 0;
         foreach ($active_sorts as $sort) {
             if ($return = $sort->sortResults($a, $b)) {
                 if ($sort->getConfiguration()['sort'] == 'DESC') {
                     $return *= -1;
                 }
                 break;
             }
         }
         return $return;
     });
     $facet->setResults($results);
     // No results behavior handling. Return a custom text or false depending on
     // settings.
     if (empty($facet->getResults())) {
         $empty_behavior = $facet->getEmptyBehavior();
         if ($empty_behavior['behavior'] == 'text') {
             return [['#markup' => $empty_behavior['text']]];
         } else {
             return [];
         }
     }
     // Let the widget plugin render the facet.
     /** @var \Drupal\facets\Widget\WidgetInterface $widget */
     $widget = $this->widgetPluginManager->createInstance($facet->getWidget());
     return [$widget->build($facet)];
 }