Commit f346b9f9 authored by borisson_'s avatar borisson_ Committed by borisson_

Issue #2974512 by borisson_, StryKaizer: Facets range widget directly alters the url

parent e6875a62
......@@ -46,25 +46,15 @@ class RangeSliderProcessor extends SliderProcessor implements PreQueryProcessorI
/** @var \Drupal\facets\Plugin\facets\processor\UrlProcessorHandler $url_processor_handler */
$url_processor_handler = $facet->getProcessors()['url_processor_handler'];
$url_processor = $url_processor_handler->getProcessor();
$filter_key = $url_processor->getFilterKey();
$active_filters = $url_processor->getActiveFilters();
/** @var \Drupal\facets\Result\ResultInterface[] $results */
foreach ($results as &$result) {
$url = $result->getUrl();
$query = $url->getOption('query');
// Remove all the query filters for the field of the facet.
if ($query !== NULL) {
foreach ($query[$filter_key] as $id => $filter) {
if (strpos($filter . $url_processor->getSeparator(), $facet->getUrlAlias()) === 0) {
unset($query[$filter_key][$id]);
}
}
}
$new_active_filters = $active_filters;
unset($new_active_filters[$facet->getUrlAlias()]);
// Add one generic query filter with the min and max placeholder.
$query[$filter_key][] = $facet->getUrlAlias() . $url_processor->getSeparator() . '(min:__range_slider_min__,max:__range_slider_max__)';
$url->setOption('query', $query);
$new_active_filters[$facet->getUrlAlias()][] = '(min:__range_slider_min__,max:__range_slider_max__)';
$url = \Drupal::service('facets.utility.url_generator')->getUrl($new_active_filters, FALSE);
$result->setUrl($url);
}
......
......@@ -10,8 +10,10 @@ use Drupal\facets\Entity\Facet;
use Drupal\facets\Plugin\facets\processor\UrlProcessorHandler;
use Drupal\facets\Plugin\facets\url_processor\QueryString;
use Drupal\facets\Result\Result;
use Drupal\facets\Utility\FacetsUrlGenerator;
use Drupal\facets_range_widget\Plugin\facets\processor\RangeSliderProcessor;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
/**
* Unit test for processor.
......@@ -35,9 +37,13 @@ class RangeSliderProcessorTest extends UnitTestCase {
parent::setUp();
$this->processor = new RangeSliderProcessor([], 'range_slider', []);
$facets_url_generator = $this->prophesize(FacetsUrlGenerator::class);
$facets_url_generator->getUrl(Argument::any(), Argument::any())->willReturn(new Url('test', [], ['query' => ['f' => ['animals::(min:__range_slider_min__,max:__range_slider_max__)']]]));
$url_generator = $this->prophesize(UrlGeneratorInterface::class);
$container = new ContainerBuilder();
$container->set('url_generator', $url_generator->reveal());
$container->set('facets.utility.url_generator', $facets_url_generator->reveal());
\Drupal::setContainer($container);
}
......@@ -66,6 +72,7 @@ class RangeSliderProcessorTest extends UnitTestCase {
$queryString = $this->prophesize(QueryString::class);
$queryString->getFilterKey()->willReturn('f');
$queryString->getSeparator()->willReturn('::');
$queryString->getActiveFilters()->willReturn([]);
$urlHandler = $this->prophesize(UrlProcessorHandler::class);
$urlHandler->getProcessor()->willReturn($queryString->reveal());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment