You are here

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

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

File

src/Plugin/facets/processor/GlossaryAZWidgetOrderProcessor.php, line 68

Class

GlossaryAZWidgetOrderProcessor
A processor that orders the results by display value.

Namespace

Drupal\search_api_glossary\Plugin\facets\processor

Code

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

  // Get the custom sort order from config.
  $sort_options_by_weight = $this
    ->sortConfigurationWeight($this
    ->getConfiguration()['sort']);

  // Initialise an empty array and populate
  // it with options in the same order as the sort
  // order defined in the config.
  $glossary_results = array();
  foreach ($sort_options_by_weight as $sort_option_by_weight_id => $sort_option_by_weight_weight) {
    $glossary_results[$sort_option_by_weight_id] = array();
  }

  // Since our new array is already in
  // the sort order defined in the config
  // lets step through the results and populate
  // results into respective containers.
  foreach ($results as $result) {

    // Is it a number? or maybe grouped number eg 0-9 (technically a string).
    if ($result
      ->getRawValue() == '0-9' || ctype_digit($result
      ->getRawValue()) || is_int($result
      ->getRawValue())) {
      $glossary_results['glossaryaz_sort_09'][$result
        ->getRawValue()] = $result;
    }
    elseif ($result
      ->getRawValue() == 'All') {
      $glossary_results['glossaryaz_sort_all'][$result
        ->getRawValue()] = $result;
    }
    elseif (ctype_alpha($result
      ->getRawValue())) {
      $glossary_results['glossaryaz_sort_az'][$result
        ->getRawValue()] = $result;
    }
    else {
      $glossary_results['glossaryaz_sort_other'][$result
        ->getRawValue()] = $result;
    }
  }
  ksort($glossary_results['glossaryaz_sort_az']);
  ksort($glossary_results['glossaryaz_sort_09']);
  ksort($glossary_results['glossaryaz_sort_other']);

  // Flatten the array to same structure as $results.
  $glossary_results_sorted = array();
  foreach ($glossary_results as $glossary_result) {
    if ($glossary_result) {
      $glossary_results_sorted = array_merge($glossary_results_sorted, $glossary_result);
    }
  }

  // And its done.
  return $glossary_results_sorted;
}