You are here

public function SliderProcessor::postQuery in Facets 8

Runs after the query was executed.

Uses the query results and can alter those results, for example a ValueCallbackProcessor. If results are being changed, this processor should handle saving itself.

Parameters

\Drupal\facets\FacetInterface $facet: The facet that's being changed.

Overrides PostQueryProcessorInterface::postQuery

File

modules/facets_range_widget/src/Plugin/facets/processor/SliderProcessor.php, line 27

Class

SliderProcessor
Provides a processor that adds all values between an min and max range.

Namespace

Drupal\facets_range_widget\Plugin\facets\processor

Code

public function postQuery(FacetInterface $facet) {
  $widget = $facet
    ->getWidgetInstance();
  $config = $widget
    ->getConfiguration();
  $simple_results = [];

  // Generate all the "results" between min and max, with the configured step.
  foreach ($facet
    ->getResults() as $result) {
    $simple_results['f_' . (double) $result
      ->getRawValue()] = [
      'value' => (double) $result
        ->getRawValue(),
      'count' => (int) $result
        ->getCount(),
    ];
  }
  uasort($simple_results, function ($a, $b) {
    if ($a['value'] === $b['value']) {
      return 0;
    }
    return $a['value'] < $b['value'] ? -1 : 1;
  });
  $step = $config['step'];
  if ($config['min_type'] == 'fixed') {
    $min = $config['min_value'];
    $max = $config['max_value'];
  }
  else {
    $min = reset($simple_results)['value'] ?? 0;
    $max = end($simple_results)['value'] ?? 0;

    // If max is not divisible by step, we should add the remainder to max to
    // make sure that we don't lose any possible values.
    if ($max % $step !== 0) {
      $max = $max + ($step - $max % $step);
    }
  }

  // Creates an array of all results between min and max by the step from the
  // configuration.
  $new_results = [];
  for ($i = $min; $i <= $max; $i += $step) {
    $count = isset($simple_results['f_' . $i]) ? $simple_results['f_' . $i]['count'] : 0;
    $new_results[] = new Result($facet, (double) $i, (double) $i, $count);
  }

  // Overwrite the current facet values with the generated results.
  $facet
    ->setResults($new_results);
}