You are here

public function GlossaryAZPadItemsProcessor::build in Search API AZ Glossary 8

Same name and namespace in other branches
  1. 8.4 src/Plugin/facets/processor/GlossaryAZPadItemsProcessor.php \Drupal\search_api_glossary\Plugin\facets\processor\GlossaryAZPadItemsProcessor::build()
  2. 8.2 src/Plugin/facets/processor/GlossaryAZPadItemsProcessor.php \Drupal\search_api_glossary\Plugin\facets\processor\GlossaryAZPadItemsProcessor::build()
  3. 8.3 src/Plugin/facets/processor/GlossaryAZPadItemsProcessor.php \Drupal\search_api_glossary\Plugin\facets\processor\GlossaryAZPadItemsProcessor::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/GlossaryAZPadItemsProcessor.php, line 27

Class

GlossaryAZPadItemsProcessor
Provides a processor to rewrite facet results to pad out missing alpha.

Namespace

Drupal\search_api_glossary\Plugin\facets\processor

Code

public function build(FacetInterface $facet, array $results) {
  $glossary_field_id = $facet
    ->getFieldIdentifier();

  // Load up config and loop though settings.
  $config = \Drupal::config('search_api_glossary.settings');
  $glossary_field_settings = $config
    ->get($glossary_field_id);

  // Is this a glossary field?
  if ($glossary_field_settings == NULL) {
    return $results;
  }
  $glossary_az_grouping = array_values($glossary_field_settings['glossary_az_grouping']);
  $glossary_array = array();

  // If Alpha grouping is not set, pad alpha.
  if (!in_array('glossary_az_grouping_az', $glossary_az_grouping, TRUE)) {
    $glossary_array = array_merge($glossary_array, range('A', 'Z'));
  }
  else {
    $glossary_array[] = "A-Z";
  }

  // If Numeric grouping is not set, pad alpha.
  if (!in_array('glossary_az_grouping_09', $glossary_az_grouping, TRUE)) {
    $glossary_array = array_merge($glossary_array, array_map('strval', range('0', '9')));
  }
  else {
    $glossary_array[] = "0-9";
  }

  // Do we have Non Alpha Numeric grouping?
  if (in_array('glossary_az_grouping_other', $glossary_az_grouping, TRUE)) {
    $glossary_array[] = "#";
  }

  // Generate keys from values.
  $glossary_missing = array_combine($glossary_array, $glossary_array);

  /** @var \Drupal\facets\Result\ResultInterface $result */
  foreach ($results as $result) {
    $result_glossary = $result
      ->getDisplayValue();

    // Items that exist in result, remove them from sample array.
    if (in_array($result_glossary, $glossary_missing)) {
      unset($glossary_missing[$result_glossary]);
    }
  }

  // Loop over the missing items and add them.
  foreach ($glossary_missing as $glossary) {
    $results[] = new Result($glossary, $glossary, 0);
  }
  return $results;
}