diff --git a/src/Form/CheckboxWidgetForm.php b/src/Form/CheckboxWidgetForm.php index 33beff9..36676a0 100644 --- a/src/Form/CheckboxWidgetForm.php +++ b/src/Form/CheckboxWidgetForm.php @@ -96,40 +96,20 @@ class CheckboxWidgetForm implements BaseFormIdInterface { $values = $form_state->getValues(); $facet = $this->facet; - $result_link = FALSE; $active_items = []; - foreach ($values[$facet->getFieldAlias()] as $key => $value) { if ($value !== 0) { $active_items[] = $value; } } - foreach ($facet->getResults() as $result) { - if (in_array($result->getRawValue(), $active_items)) { - $result_link = $result->getUrl(); - } - } - - // We have an active item, so we redirect to the page that has that facet - // selected. This should be an absolute link because RedirectResponse is a - // symfony class that requires a full URL. - if ($result_link instanceof Url) { - $result_link->setAbsolute(); - $form_state->setResponse(new RedirectResponse($result_link->toString())); - return; - } + $url_processor_name = $facet->getUrlProcessorName(); + $manager = \Drupal::getContainer()->get('plugin.manager.facets.url_processor'); + $url_processor = $manager->createInstance($url_processor_name, ['facet' => $facet]); - // The form was submitted but nothing was active in the form, we should - // still redirect, but the url for the new page can't come from a result. - // So we're redirecting to the facet source's page. - $path = $facet->getFacetSource()->getPath(); - if (substr($path, 0, 1) !== '/') { - $path = '/' . $path; - } - $link = Url::fromUserInput($path); - $link->setAbsolute(); - $form_state->setResponse(new RedirectResponse($link->toString())); + $result_link = $url_processor->getUrlMultipleItemsAtOnce($facet, $active_items); + $result_link->setAbsolute(); + $form_state->setResponse(new RedirectResponse($result_link->toString())); } } diff --git a/src/Plugin/facets/url_processor/QueryString.php b/src/Plugin/facets/url_processor/QueryString.php index 125cc1a..07abd56 100644 --- a/src/Plugin/facets/url_processor/QueryString.php +++ b/src/Plugin/facets/url_processor/QueryString.php @@ -49,6 +49,46 @@ class QueryString extends UrlProcessorPluginBase { /** * {@inheritdoc} */ + public function getUrlMultipleItemsAtOnce(FacetInterface $facet, array $current_facet_enabled_items){ + // Set the url alias from the the facet object. + $this->urlAlias = $facet->getUrlAlias(); + + // First get the current list of get parameters. + $get_params = $this->request->query; + $filter_params = $get_params->get($this->filterKey, [], TRUE); + + // Remove possible items from current facet first. + foreach($filter_params as $key => $value){ + $parts = explode( self::SEPARATOR , $value); + if($parts[0] == $this->urlAlias){ + unset($filter_params[$key]); + } + } + + // Enable only items from $enabled_items (again) for current facet. + foreach($current_facet_enabled_items as $enabled_item){ + $filter_string = $this->urlAlias . self::SEPARATOR . $enabled_item; + $filter_params[] = $filter_string; + } + + $request = $this->request; + if ($facet->getFacetSource()->getPath()) { + $request = Request::create($facet->getFacetSource()->getPath()); + } + $url = Url::createFromRequest($request); + + + $result_get_params = clone $get_params; + $result_get_params->set($this->filterKey, array_values($filter_params)); + $url = clone $url; + $url->setOption('query', $result_get_params->all()); + + return $url; + } + + /** + * {@inheritdoc} + */ public function buildUrls(FacetInterface $facet, array $results) { // No results are found for this facet, so don't try to create urls. if (empty($results)) { diff --git a/src/UrlProcessor/UrlProcessorInterface.php b/src/UrlProcessor/UrlProcessorInterface.php index e014676..7581595 100644 --- a/src/UrlProcessor/UrlProcessorInterface.php +++ b/src/UrlProcessor/UrlProcessorInterface.php @@ -50,4 +50,23 @@ interface UrlProcessorInterface { */ public function getFilterKey(); + /** + * Gets the url for changing multiple items at once for one facet specific. + * + * Certain widgets (e.g. the checkbox widget) can enable/disable multiple + * items at once in one request. This function allows building urls for those + * widgets. Note, the $current_facet_enabled_items contains only the enabled + * items for the given facet. There can be other enabled items, as should be + * determined by the url processor. + * + * @param \Drupal\facets\FacetInterface $facet + * The facet that is changing certain items. + * @param array $current_facet_enabled_items + * Array of enabled raw values for the current facet. + * + * @return \Drupal\Core\Url + * Url to filter on enabled items for given facet. + */ + public function getUrlMultipleItemsAtOnce(FacetInterface $facet, array $current_facet_enabled_items); + } diff --git a/tests/facets_query_processor/src/Plugin/facets/url_processor/DummyQuery.php b/tests/facets_query_processor/src/Plugin/facets/url_processor/DummyQuery.php index 8eead55..d217a88 100644 --- a/tests/facets_query_processor/src/Plugin/facets/url_processor/DummyQuery.php +++ b/tests/facets_query_processor/src/Plugin/facets/url_processor/DummyQuery.php @@ -135,4 +135,42 @@ class DummyQuery extends UrlProcessorPluginBase { } } + /** + * {@inheritdoc} + */ + public function getUrlMultipleItemsAtOnce(FacetInterface $facet, array $current_facet_enabled_items){ + // Set the url alias from the the facet object. + $this->urlAlias = $facet->getUrlAlias(); + + // First get the current list of get parameters. + $get_params = $this->request->query; + $filter_params = $get_params->get($this->filterKey, [], TRUE); + + foreach($filter_params as $key => $value){ + $parts = explode( self::SEPARATOR , $value); + if($parts[0] == $this->urlAlias){ + unset($filter_params[$key]); + } + } + + foreach($current_facet_enabled_items as $enabled_item){ + $filter_string = $this->urlAlias . self::SEPARATOR . $enabled_item; + $filter_params[] = $filter_string; + } + + $request = $this->request; + if ($facet->getFacetSource()->getPath()) { + $request = Request::create($facet->getFacetSource()->getPath()); + } + $url = Url::createFromRequest($request); + + + $result_get_params = clone $get_params; + $result_get_params->set($this->filterKey, array_values($filter_params)); + $url = clone $url; + $url->setOption('query', $result_get_params->all()); + + return $url; + } + }