Commit 872fd435 authored by brentgees's avatar brentgees Committed by borisson_

Issue #2896854 by brentgees, borisson_: Add option to hide facets with only one option

parent 0bfda3c6
<?php
namespace Drupal\facets\Plugin\facets\processor;
use Drupal\facets\FacetInterface;
use Drupal\facets\Processor\BuildProcessorInterface;
use Drupal\facets\Processor\ProcessorPluginBase;
/**
* Provides a processor that removes all results when the set has only 1 item.
*
* @FacetsProcessor(
* id = "hide_1_result_facet",
* label = @Translation("Hide facet with 1 result"),
* description = @Translation("When the facet has only one result, it will be hidden"),
* stages = {
* "build" = 50
* }
* )
*/
class HideOnlyOneItemProcessor extends ProcessorPluginBase implements BuildProcessorInterface {
/**
* {@inheritdoc}
*/
public function build(FacetInterface $facet, array $results) {
if (count($results) === 1) {
return [];
}
return $results;
}
}
......@@ -667,4 +667,40 @@ class ProcessorIntegrationTest extends FacetsTestBase {
$this->assertSession()->pageTextNotContains('test pre query');
}
/**
* Test HideOnlyOneItemProcessor.
*
* Test if after clicking an item that has only one item, the facet block no
* longer shows.
*/
public function testHideOnlyOneItemProcessor() {
$entity_test_storage = \Drupal::entityTypeManager()
->getStorage('entity_test_mulrev_changed');
$entity_test_storage->create([
'name' => 'baz baz',
'body' => 'foo test',
'type' => 'article',
'keywords' => ['kiwi'],
'category' => 'article_category',
])->save();
$this->indexItems($this->indexId);
$facet_name = 'Drupalcon Vienna';
$facet_id = 'drupalcon_vienna';
$this->editForm = 'admin/config/search/facets/' . $facet_id . '/edit';
$this->createFacet($facet_name, $facet_id, 'keywords');
$form = [
'facet_settings[hide_1_result_facet][status]' => TRUE,
'facet_settings[query_operator]' => 'and',
];
$this->drupalPostForm($this->editForm, $form, 'Save');
$this->drupalGet('search-api-test-fulltext');
$this->assertFacetBlocksAppear();
$this->clickLink('kiwi');
$this->assertNoFacetBlocksAppear();
}
}
<?php
namespace Drupal\Tests\facets\Unit\Plugin\processor;
use Drupal\facets\Entity\Facet;
use Drupal\facets\Plugin\facets\processor\HideOnlyOneItemProcessor;
use Drupal\facets\Result\Result;
use Drupal\Tests\UnitTestCase;
/**
* Class HideOnlyOneItemProcessorTest.
*
* @coversDefaultClass Drupal\facets\Plugin\facets\processor\HideOnlyOneItemProcessor
* @group facets
*/
class HideOnlyOneItemProcessorTest extends UnitTestCase {
/**
* Tests with one result.
*
* @covers ::build
*/
public function testWithOneResult() {
$processor = new HideOnlyOneItemProcessor([], 'hide_only_one_item', []);
$facet = new Facet([], 'facets_facet');
$results = [
new Result($facet, '1', 1, 1),
];
$facet = $this->getMockBuilder(Facet::class)
->disableOriginalConstructor()
->getMock();
$processed_results = $processor->build($facet, $results);
$this->assertCount(0, $processed_results);
}
/**
* Tests with one result.
*
* @covers ::build
*/
public function testWithMoreResults() {
$processor = new HideOnlyOneItemProcessor([], 'hide_only_one_item', []);
$facet = new Facet([], 'facets_facet');
$results = [
new Result($facet, '1', 1, 1),
new Result($facet, '2', 2, 2),
];
$facet = $this->getMockBuilder(Facet::class)
->disableOriginalConstructor()
->getMock();
$processed_results = $processor->build($facet, $results);
$this->assertCount(2, $processed_results);
}
}
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