diff --git a/src/Plugin/facets/url_processor/QueryString.php b/src/Plugin/facets/url_processor/QueryString.php index 346b294..78812b5 100644 --- a/src/Plugin/facets/url_processor/QueryString.php +++ b/src/Plugin/facets/url_processor/QueryString.php @@ -69,7 +69,7 @@ class QueryString extends UrlProcessorPluginBase { /** @var \Drupal\facets\Result\ResultInterface $result */ foreach ($results as &$result) { - $filter_string = $this->url_alias . ':' . $result->getRawValue(); + $filter_string = $this->url_alias . self::SEPARATOR . $result->getRawValue(); $result_get_params = clone $get_params; $filter_params = $result_get_params->get($this->filterKey, [], TRUE); diff --git a/src/Tests/UrlIntegrationTest.php b/src/Tests/UrlIntegrationTest.php new file mode 100644 index 0000000..62339d3 --- /dev/null +++ b/src/Tests/UrlIntegrationTest.php @@ -0,0 +1,104 @@ +drupalLogin($this->adminUser); + + $this->insertExampleContent(); + $this->assertEqual($this->indexItems($this->indexId), 5, '5 items were indexed.'); + + $id = 'facet'; + $name = '&^Facet@#1'; + + $facet_add_page = 'admin/config/search/facets/add-facet'; + $this->drupalGet($facet_add_page); + $this->assertResponse(200); + + $form_values = [ + 'id' => $id, + 'status' => 1, + 'url_alias' => $id, + 'name' => $name, + 'facet_source_id' => 'search_api_views:search_api_test_views_fulltext:page_1', + 'facet_source_configs[search_api_views:search_api_test_views_fulltext:page_1][field_identifier]' => 'entity:entity_test/type', + ]; + $this->drupalPostForm(NULL, ['facet_source_id' => 'search_api_views:search_api_test_views_fulltext:page_1'], $this->t('Configure facet source')); + $this->drupalPostForm(NULL, $form_values, $this->t('Save')); + $this->assertNoText('field is required.'); + + $this->assertRaw(t('Facet %name has been created.', ['%name' => $name])); + + $this->drupalGet('admin/config/search/facets'); + + $edit = [ + 'filterKey' => 'y', + 'urlProcessor' => 'dummy_query', + ]; + $this->clickLink($this->t('Configure')); + $this->drupalPostForm(NULL, $edit, $this->t('Save')); + + /** @var \Drupal\facets\FacetInterface $facet */ + $facet = \Drupal::service('entity_type.manager')->getStorage('facets_facet')->load($id); + $block_values = [ + [ + 'label' => 'Facet Block', + 'tr' => '16', + 'plugin_id' => 'facet_block', + 'settings' => [ + 'region' => 'footer', + 'id' => str_replace('_', '-', $id), + 'context_mapping' => [ + 'facet' => '@facets.facet_context:' . $facet->uuid(), + ], + ], + 'test_weight' => '0', + ], + ]; + foreach ($block_values as $values) { + $this->drupalPlaceBlock($values['plugin_id'], $values['settings']); + } + + $this->drupalGet('search-api-test-fulltext'); + $this->assertLink('item'); + $this->assertLink('article'); + + $this->clickLink('item'); + + $this->assertLink('(-) item'); + $this->assertUrl('search-api-test-fulltext?y[0]=facet||item'); + } + +} diff --git a/tests/facets_query_processor/facets_query_processor.info.yml b/tests/facets_query_processor/facets_query_processor.info.yml new file mode 100644 index 0000000..8347423 --- /dev/null +++ b/tests/facets_query_processor/facets_query_processor.info.yml @@ -0,0 +1,6 @@ +name: 'Facets query processor' +type: module +description: 'Facets query processor' +package: 'Search' +core: 8.x +hidden: true 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 new file mode 100644 index 0000000..764e67c --- /dev/null +++ b/tests/facets_query_processor/src/Plugin/facets/url_processor/DummyQuery.php @@ -0,0 +1,143 @@ +initializeActiveFilters(); + } + + /** + * {@inheritdoc} + */ + public function buildUrls(FacetInterface $facet, array $results) { + // Create links for all the values. + // First get the current list of get parameters. + $get_params = $this->request->query; + + // Set the url alias from the the facet object. + $this->url_alias = $facet->getUrlAlias(); + + // No results are found for this facet, so don't try to create urls. + if (empty($results)) { + return []; + } + + /** @var \Drupal\facets\Result\ResultInterface $result */ + foreach ($results as &$result) { + $filter_string = $this->url_alias . self::SEPARATOR . $result->getRawValue(); + $result_get_params = clone $get_params; + + $filter_params = $result_get_params->get($this->filterKey, [], TRUE); + // If the value is active, remove the filter string from the parameters. + if ($result->isActive()) { + foreach ($filter_params as $key => $filter_param) { + if ($filter_param == $filter_string) { + unset($filter_params[$key]); + } + } + } + // If the value is not active, add the filter string. + else { + $filter_params[] = $filter_string; + } + + $result_get_params->set($this->filterKey, $filter_params); + $request = $this->request; + if ($facet->getFacetSource()->getPath()) { + $request = Request::create('/' . $facet->getFacetSource()->getPath()); + } + $url = Url::createFromRequest($request); + $url->setOption('query', $result_get_params->all()); + + $result->setUrl($url); + } + + return $results; + } + + /** + * {@inheritdoc} + */ + public function setActiveItems(FacetInterface $facet) { + // Set the url alias from the the facet object. + $this->url_alias = $facet->getUrlAlias(); + + // Get the filter key of the facet. + if (isset($this->activeFilters[$this->url_alias])) { + foreach ($this->activeFilters[$this->url_alias] as $value) { + $facet->setActiveItem(trim($value, '"')); + } + } + } + + /** + * Initialize the active filters. + * + * Get all the filters that are active. This method only get's all the + * filters but doesn't assign them to facets. In the processFacet method the + * active values for a specific facet are added to the facet. + */ + protected function initializeActiveFilters() { + $url_parameters = $this->request->query; + + // Get the active facet parameters. + $active_params = $url_parameters->get($this->filterKey, array(), TRUE); + + // Explode the active params on the separator. + foreach ($active_params as $param) { + list($key, $value) = explode(self::SEPARATOR, $param); + if (!isset($this->activeFilters[$key])) { + $this->activeFilters[$key] = [$value]; + } + else { + $this->activeFilters[$key][] = $value; + } + } + } + +}