You are here

public function ShowTextWhenEmptyProcessor::build in Facets 8

Alter the items in the summary before creating the renderable array.

Parameters

\Drupal\facets_summary\FacetsSummaryInterface $facet: The facet being changed.

array $build: The render array.

\Drupal\facets\FacetInterface[] $facets: The facets that are available.

Return value

array The render array.

Overrides BuildProcessorInterface::build

File

modules/facets_summary/src/Plugin/facets_summary/processor/ShowTextWhenEmptyProcessor.php, line 28

Class

ShowTextWhenEmptyProcessor
Provides a processor that shows a text when there are no results.

Namespace

Drupal\facets_summary\Plugin\facets_summary\processor

Code

public function build(FacetsSummaryInterface $facets_summary, array $build, array $facets) {
  $config = $this
    ->getConfiguration();
  $results_count = array_sum(array_map(function ($it) {

    /** @var \Drupal\facets\FacetInterface $it */
    return count($it
      ->getResults());
  }, $facets));

  // No items are found, so we should return the empty summary.
  if (!isset($build['#items']) || $results_count === 0) {
    return [
      '#theme' => 'facets_summary_empty',
      '#message' => [
        '#type' => 'processed_text',
        '#text' => $config['text']['value'],
        '#format' => $config['text']['format'],
      ],
    ];
  }

  // Return the actual items.
  return $build;
}