You are here

public function GlossaryAZAllItemsProcessor::build in Search API AZ Glossary 8.4

Same name and namespace in other branches
  1. 8 src/Plugin/facets/processor/GlossaryAZAllItemsProcessor.php \Drupal\search_api_glossary\Plugin\facets\processor\GlossaryAZAllItemsProcessor::build()
  2. 8.2 src/Plugin/facets/processor/GlossaryAZAllItemsProcessor.php \Drupal\search_api_glossary\Plugin\facets\processor\GlossaryAZAllItemsProcessor::build()
  3. 8.3 src/Plugin/facets/processor/GlossaryAZAllItemsProcessor.php \Drupal\search_api_glossary\Plugin\facets\processor\GlossaryAZAllItemsProcessor::build()

Runs before the renderable array is created.

Parameters

\Drupal\facets\FacetInterface $facet: The facet being changed.

\Drupal\facets\Result\ResultInterface[] $results: The results being changed.

Return value

\Drupal\facets\Result\ResultInterface[] The changed results.

Overrides BuildProcessorInterface::build

File

src/Plugin/facets/processor/GlossaryAZAllItemsProcessor.php, line 64

Class

GlossaryAZAllItemsProcessor
Provides a processor to show All items in Glossary AZ.

Namespace

Drupal\search_api_glossary\Plugin\facets\processor

Code

public function build(FacetInterface $facet, array $results) {

  // Process the results count.
  $show_all_item_count = 0;

  // TODO revise this logic based on progress with
  // All items count is not correct when narrowing the results.
  // https://www.drupal.org/project/facets/issues/2692027
  // see https://git.drupalcode.org/project/facets/commit/21343a6
  foreach ($results as $result) {
    $show_all_item_count += $result
      ->getCount();
  }
  $show_all_item = new Result($facet, t('All')
    ->getUntranslatedString(), t('All'), $show_all_item_count);

  // Deal with the ALL Items path.
  // See QueryString::buildUrls.
  $path = Request::create($facet
    ->getFacetSource()
    ->getPath());
  $url = Url::createFromRequest($path);

  // First get the current list of get parameters without pager.
  $get_params = new ParameterBag($this->pagerParams
    ->getQueryParameters());

  // See UrlProcessorPluginBase::__construct.
  $facet_source_config = $facet
    ->getFacetSourceConfig();
  $filterKey = $facet_source_config
    ->getFilterKey() ?: 'f';

  // See QueryString::buildUrls.
  $filter_params = $get_params
    ->get($filterKey, [], TRUE);

  // Remove the filter string from the parameters.
  foreach ($filter_params as $key => $filter_param) {
    unset($filter_params[$key]);
  }
  $get_params
    ->set($filterKey, array_values($filter_params));
  $url
    ->setOption('query', $get_params
    ->all());

  // Set the path.
  $show_all_item
    ->setUrl($url);

  // If no other facets are selected, default to ALL.
  if (empty($facet
    ->getActiveItems())) {
    $show_all_item
      ->setActiveState(TRUE);
  }

  // All done.
  $results[] = $show_all_item;
  return $results;
}