RangeSliderProcessor.php in Facets 8
File
modules/facets_range_widget/src/Plugin/facets/processor/RangeSliderProcessor.php
View source
<?php
namespace Drupal\facets_range_widget\Plugin\facets\processor;
use Drupal\facets\FacetInterface;
use Drupal\facets\Processor\BuildProcessorInterface;
use Drupal\facets\Processor\PreQueryProcessorInterface;
class RangeSliderProcessor extends SliderProcessor implements PreQueryProcessorInterface, BuildProcessorInterface {
public function preQuery(FacetInterface $facet) {
$active_items = $facet
->getActiveItems();
array_walk($active_items, function (&$item) {
if (preg_match('/\\(min:((?:-)?[\\d\\.]+),max:((?:-)?[\\d\\.]+)\\)/i', $item, $matches)) {
$item = [
$matches[1],
$matches[2],
];
}
else {
$item = NULL;
}
});
$facet
->setActiveItems($active_items);
}
public function build(FacetInterface $facet, array $results) {
$url_processor_handler = $facet
->getProcessors()['url_processor_handler'];
$url_processor = $url_processor_handler
->getProcessor();
$active_filters = $url_processor
->getActiveFilters();
if (isset($active_filters[''])) {
unset($active_filters['']);
}
foreach ($results as &$result) {
$new_active_filters = $active_filters;
unset($new_active_filters[$facet
->id()]);
$new_active_filters[$facet
->id()][] = '(min:__range_slider_min__,max:__range_slider_max__)';
$url = \Drupal::service('facets.utility.url_generator')
->getUrl($new_active_filters, FALSE);
$result
->setUrl($url);
}
return $results;
}
}