Commit b2eeb56a authored by Stefdewa's avatar Stefdewa Committed by Nick_vh

Issue #2960137 by borisson_, Sutharsan, Stefdewa: Only show reset link if...

Issue #2960137 by borisson_, Sutharsan, Stefdewa: Only show reset link if there is something to reset
parent d85475c1
......@@ -28,6 +28,7 @@ class ResetFacetsProcessor extends ProcessorPluginBase implements BuildProcessor
*/
public function build(FacetsSummaryInterface $facets_summary, array $build, array $facets) {
$configuration = $facets_summary->getProcessorConfigs()[$this->getPluginId()];
$hasReset = FALSE;
// Do nothing if there are no selected facets or reset text is empty.
if (empty($build['#items']) || empty($configuration['settings']['link_text'])) {
......@@ -46,15 +47,21 @@ class ResetFacetsProcessor extends ProcessorPluginBase implements BuildProcessor
foreach ($query_params[$filter_key] as $delta => $param) {
if (strpos($param, $url_alias . ':') !== FALSE) {
unset($query_params[$filter_key][$delta]);
$hasReset = TRUE;
}
}
if (!$query_params[$filter_key]) {
unset($query_params[$filter_key]);
$hasReset = TRUE;
}
}
}
if (!$hasReset) {
return $build;
}
$url = Url::createFromRequest($request);
$url->setOptions(['query' => $query_params]);
......
......@@ -568,4 +568,52 @@ class IntegrationTest extends FacetsTestBase {
$web_assert->pageTextContains('Test with no category');
}
/**
* Tests the reset facet link.
*
* @see https://www.drupal.org/project/facets/issues/2960137
*/
public function testResetFacetLink() {
$this->createFacet('Brasserie d\'Orval', 'orval', 'category');
$this->resetAll();
// Add a facets summary entity.
$values = [
'name' => 'Trappist beers',
'id' => 'trappist',
'facet_source_id' => 'search_api:views_page__search_api_test_view__page_1',
];
$this->drupalPostForm('admin/config/search/facets/add-facet-summary', $values, 'Save');
// Place the block.
$block = [
'region' => 'footer',
'id' => 'trappist',
'weight' => -10,
];
$summary_block = $this->drupalPlaceBlock('facets_summary_block:trappist', $block);
// Enable the facets for the summary.
$summaries = [
'facets[orval][checked]' => TRUE,
'facets[orval][weight]' => 0,
'facets_summary_settings[reset_facets][status]' => 1,
'facets_summary_settings[reset_facets][settings][link_text]' => 'Reset',
];
$this->drupalPostForm('admin/config/search/facets/facet-summary/trappist/edit', $summaries, 'Save');
// Go to the search view, and check that the summary, as well as the facets
// are shown on the page.
$this->drupalGet('search-api-test-fulltext');
$web_assert = $this->assertSession();
$web_assert->pageTextContains('Displaying 5 search results');
$this->assertFacetBlocksAppear();
$web_assert->pageTextContains($summary_block->label());
$links = $this->xpath('//a[normalize-space(text())=:label]', [':label' => 'Reset']);
$this->assertEmpty($links);
$this->clickLink('article_category');
$links = $this->xpath('//a[normalize-space(text())=:label]', [':label' => 'Reset']);
$this->assertNotEmpty($links);
}
}
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