Loading src/Event/ActiveFiltersParsed.php 0 → 100644 +96 −0 Original line number Diff line number Diff line <?php namespace Drupal\facets\Event; use Drupal\Component\EventDispatcher\Event; use Symfony\Component\HttpFoundation\ParameterBag; /** * Implements the active filters parsed event. * * This event allows modules to change the active filters parsed from URL if * needed. */ final class ActiveFiltersParsed extends Event { /** * The facet source id. * * @var string */ private $facetsource_id; /** * The active filters. * * @var array */ private $activeFilters; /** * The get parameters. * * @var \Symfony\Component\HttpFoundation\ParameterBag */ private $queryParameters; /** * QueryStringCreated constructor. * * @param string $facetsource_id * The facet source id. * @param array $activeFilters * The active filters. * @param \Symfony\Component\HttpFoundation\ParameterBag $queryParameters * The get parameters to use. */ public function __construct($facetsource_id, array $activeFilters, ParameterBag $queryParameters) { $this->facetsource_id = $facetsource_id; $this->queryParameters = $queryParameters; $this->activeFilters = $activeFilters; } /** * Get the facet source id. * * @return string * The facet source id. */ public function getFacetSourceId() { return $this->facetsource_id; } /** * Get the active filters. * * Only to be used as context, because changing this will not result in any * changes to the final url. * * @return array * The active filters. */ public function getActiveFilters() { return $this->activeFilters; } /** * Set the active filters. * * @param array $activeFilters * The active filters. */ public function setActiveFilters(array $activeFilters) { $this->activeFilters = $activeFilters; } /** * Get the get parameters. * * @return \Symfony\Component\HttpFoundation\ParameterBag * The get parameters. */ public function getQueryParameters() { return $this->queryParameters; } } src/Event/FacetsEvents.php +9 −0 Original line number Diff line number Diff line Loading @@ -16,4 +16,13 @@ final class FacetsEvents { */ public const QUERY_STRING_CREATED = QueryStringCreated::class; /** * This event allows modules to change the active filters after parsing them. * * @Event * * @see \Drupal\facets\Event\ActiveFiltersParsed */ public const ACTIVE_FILTERS_PARSED = ActiveFiltersParsed::class; } src/Plugin/facets/url_processor/QueryString.php +20 −12 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ namespace Drupal\facets\Plugin\facets\url_processor; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\EventSubscriber\MainContentViewSubscriber; use Drupal\Core\Url; use Drupal\facets\Event\ActiveFiltersParsed; use Drupal\facets\Event\QueryStringCreated; use Drupal\facets\FacetInterface; use Drupal\facets\UrlProcessor\UrlProcessorPluginBase; Loading Loading @@ -330,11 +331,12 @@ class QueryString extends UrlProcessorPluginBase { return; } $active_filters = []; // Explode the active params on the separator. foreach ($active_params as $param) { $explosion = explode($this->getSeparator(), $param); $url_alias = array_shift($explosion); $facet_id = $this->getFacetIdByUrlAlias($url_alias, $facet_source_id); if ($facet_id = $this->getFacetIdByUrlAlias($url_alias, $facet_source_id)) { $value = ''; while (count($explosion) > 0) { $value .= array_shift($explosion); Loading @@ -342,15 +344,21 @@ class QueryString extends UrlProcessorPluginBase { $value .= $this->getSeparator(); } } if (!isset($this->activeFilters[$facet_id])) { $this->activeFilters[$facet_id] = [$value]; if (!isset($active_filters[$facet_id])) { $active_filters[$facet_id] = [$value]; } else { $this->activeFilters[$facet_id][] = $value; $active_filters[$facet_id][] = $value; } } } // Allow other modules to alter the parsed active filters. $event = new ActiveFiltersParsed($facet_source_id, $active_filters, $this->request->query); $this->eventDispatcher->dispatch($event); $this->activeFilters = $event->getActiveFilters(); } /** * Gets the facet id from the url alias & facet source id. * Loading tests/src/Unit/Plugin/url_processor/QueryStringTest.php +2 −2 Original line number Diff line number Diff line Loading @@ -203,11 +203,11 @@ class QueryStringTest extends UnitTestCase { * Tests with an active item already from url. */ public function testBuildWithActiveItem() { $facet = new Facet([], 'facets_facet'); $facet = new Facet(['id' => 'facet_1'], 'facets_facet'); $facet->setFieldIdentifier('test'); $facet->setUrlAlias('test'); $facet->setFacetSourceId('facet_source__dummy'); $facet2 = new Facet([], 'facets_facet'); $facet2 = new Facet(['id' => 'facet_2'], 'facets_facet'); $facet2->setFieldIdentifier('king'); $facet2->setUrlAlias('king'); $facet2->setFacetSourceId('facet_source__dummy'); Loading Loading
src/Event/ActiveFiltersParsed.php 0 → 100644 +96 −0 Original line number Diff line number Diff line <?php namespace Drupal\facets\Event; use Drupal\Component\EventDispatcher\Event; use Symfony\Component\HttpFoundation\ParameterBag; /** * Implements the active filters parsed event. * * This event allows modules to change the active filters parsed from URL if * needed. */ final class ActiveFiltersParsed extends Event { /** * The facet source id. * * @var string */ private $facetsource_id; /** * The active filters. * * @var array */ private $activeFilters; /** * The get parameters. * * @var \Symfony\Component\HttpFoundation\ParameterBag */ private $queryParameters; /** * QueryStringCreated constructor. * * @param string $facetsource_id * The facet source id. * @param array $activeFilters * The active filters. * @param \Symfony\Component\HttpFoundation\ParameterBag $queryParameters * The get parameters to use. */ public function __construct($facetsource_id, array $activeFilters, ParameterBag $queryParameters) { $this->facetsource_id = $facetsource_id; $this->queryParameters = $queryParameters; $this->activeFilters = $activeFilters; } /** * Get the facet source id. * * @return string * The facet source id. */ public function getFacetSourceId() { return $this->facetsource_id; } /** * Get the active filters. * * Only to be used as context, because changing this will not result in any * changes to the final url. * * @return array * The active filters. */ public function getActiveFilters() { return $this->activeFilters; } /** * Set the active filters. * * @param array $activeFilters * The active filters. */ public function setActiveFilters(array $activeFilters) { $this->activeFilters = $activeFilters; } /** * Get the get parameters. * * @return \Symfony\Component\HttpFoundation\ParameterBag * The get parameters. */ public function getQueryParameters() { return $this->queryParameters; } }
src/Event/FacetsEvents.php +9 −0 Original line number Diff line number Diff line Loading @@ -16,4 +16,13 @@ final class FacetsEvents { */ public const QUERY_STRING_CREATED = QueryStringCreated::class; /** * This event allows modules to change the active filters after parsing them. * * @Event * * @see \Drupal\facets\Event\ActiveFiltersParsed */ public const ACTIVE_FILTERS_PARSED = ActiveFiltersParsed::class; }
src/Plugin/facets/url_processor/QueryString.php +20 −12 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ namespace Drupal\facets\Plugin\facets\url_processor; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\EventSubscriber\MainContentViewSubscriber; use Drupal\Core\Url; use Drupal\facets\Event\ActiveFiltersParsed; use Drupal\facets\Event\QueryStringCreated; use Drupal\facets\FacetInterface; use Drupal\facets\UrlProcessor\UrlProcessorPluginBase; Loading Loading @@ -330,11 +331,12 @@ class QueryString extends UrlProcessorPluginBase { return; } $active_filters = []; // Explode the active params on the separator. foreach ($active_params as $param) { $explosion = explode($this->getSeparator(), $param); $url_alias = array_shift($explosion); $facet_id = $this->getFacetIdByUrlAlias($url_alias, $facet_source_id); if ($facet_id = $this->getFacetIdByUrlAlias($url_alias, $facet_source_id)) { $value = ''; while (count($explosion) > 0) { $value .= array_shift($explosion); Loading @@ -342,15 +344,21 @@ class QueryString extends UrlProcessorPluginBase { $value .= $this->getSeparator(); } } if (!isset($this->activeFilters[$facet_id])) { $this->activeFilters[$facet_id] = [$value]; if (!isset($active_filters[$facet_id])) { $active_filters[$facet_id] = [$value]; } else { $this->activeFilters[$facet_id][] = $value; $active_filters[$facet_id][] = $value; } } } // Allow other modules to alter the parsed active filters. $event = new ActiveFiltersParsed($facet_source_id, $active_filters, $this->request->query); $this->eventDispatcher->dispatch($event); $this->activeFilters = $event->getActiveFilters(); } /** * Gets the facet id from the url alias & facet source id. * Loading
tests/src/Unit/Plugin/url_processor/QueryStringTest.php +2 −2 Original line number Diff line number Diff line Loading @@ -203,11 +203,11 @@ class QueryStringTest extends UnitTestCase { * Tests with an active item already from url. */ public function testBuildWithActiveItem() { $facet = new Facet([], 'facets_facet'); $facet = new Facet(['id' => 'facet_1'], 'facets_facet'); $facet->setFieldIdentifier('test'); $facet->setUrlAlias('test'); $facet->setFacetSourceId('facet_source__dummy'); $facet2 = new Facet([], 'facets_facet'); $facet2 = new Facet(['id' => 'facet_2'], 'facets_facet'); $facet2->setFieldIdentifier('king'); $facet2->setUrlAlias('king'); $facet2->setFacetSourceId('facet_source__dummy'); Loading