Commit e9857240 authored by borisson_'s avatar borisson_ Committed by borisson_

Issue #2613576 by borisson_, Novitsh: Add an option to toggle whether the facet count is displayed

parent 13e98827
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
namespace Drupal\facetapi\Plugin\facetapi\widget; namespace Drupal\facetapi\Plugin\facetapi\widget;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\facetapi\FacetInterface; use Drupal\facetapi\FacetInterface;
use Drupal\facetapi\Widget\WidgetInterface; use Drupal\facetapi\Widget\WidgetInterface;
...@@ -21,6 +22,8 @@ use Drupal\facetapi\Widget\WidgetInterface; ...@@ -21,6 +22,8 @@ use Drupal\facetapi\Widget\WidgetInterface;
*/ */
class LinksWidget implements WidgetInterface { class LinksWidget implements WidgetInterface {
use StringTranslationTrait;
/** /**
* @var \Drupal\Core\Utility\LinkGeneratorInterface $linkGenerator * @var \Drupal\Core\Utility\LinkGeneratorInterface $linkGenerator
*/ */
...@@ -40,10 +43,17 @@ class LinksWidget implements WidgetInterface { ...@@ -40,10 +43,17 @@ class LinksWidget implements WidgetInterface {
/** @var \Drupal\facetapi\Result\Result[] $results */ /** @var \Drupal\facetapi\Result\Result[] $results */
$results = $facet->getResults(); $results = $facet->getResults();
$items = []; $items = [];
$configuration = $facet->get('widget_configs');
$show_numbers = (bool) $configuration['show_numbers'];
foreach ($results as $result) { foreach ($results as $result) {
if ($result->getCount()) { if ($result->getCount()) {
// Get the link. // Get the link.
$text = $result->getDisplayValue() . ' (' . $result->getCount() . ')'; $text = $result->getDisplayValue();
if ($show_numbers) {
$text .= ' (' . $result->getCount() . ')';
}
if ($result->isActive()) { if ($result->isActive()) {
$text = '(-) ' . $text; $text = '(-) ' . $text;
} }
...@@ -64,13 +74,28 @@ class LinksWidget implements WidgetInterface { ...@@ -64,13 +74,28 @@ class LinksWidget implements WidgetInterface {
return $build; return $build;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function buildConfigurationForm(array $form, FormStateInterface $form_state) { public function buildConfigurationForm(array $form, FormStateInterface $form_state, $config) {
return FALSE;
$form['show_numbers'] = [
'#type' => 'checkbox',
'#title' => $this->t('Show the amount of results'),
];
if (!is_null($config)) {
$widget_configs = $config->get('widget_configs');
if (isset($widget_configs['show_numbers'])) {
$form['show_numbers']['#default_value'] = $widget_configs['show_numbers'];
}
}
return $form;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
...@@ -79,23 +79,24 @@ class IntegrationTest extends FacetWebTestBase { ...@@ -79,23 +79,24 @@ class IntegrationTest extends FacetWebTestBase {
// Verify that the facet results are correct. // Verify that the facet results are correct.
$this->drupalGet('search-api-test-fulltext'); $this->drupalGet('search-api-test-fulltext');
$this->assertText('item (3)'); $this->assertText('item');
$this->assertText('article (2)'); $this->assertText('article');
// Verify that facet blocks appear as expected. // Verify that facet blocks appear as expected.
$this->assertFacetBlocksAppear(); $this->assertFacetBlocksAppear();
// Verify that the facet is visible when removing a facet for example. // Verify that the facet is visible when removing a facet for example.
$this->goToDeleteFacetPage("Test Facet Name"); $this->goToDeleteFacetPage("Test Facet Name");
$this->assertText('item (3)'); $this->assertText('item');
$this->assertText('article (2)'); $this->assertText('article');
// Show the facet only when the facet source is visible. // Show the facet only when the facet source is visible.
// @TODO Only for SearchApiViewsPage for the moment. // @TODO Only for SearchApiViewsPage for the moment.
$this->setOptionShowOnlyWhenFacetSourceVisible("Test Facet name"); $this->setOptionShowOnlyWhenFacetSourceVisible("Test Facet name");
$this->goToDeleteFacetPage("Test Facet Name"); $this->goToDeleteFacetPage("Test Facet Name");
$this->assertNoText('item (3)'); $this->assertNoText('item');
$this->assertNoText('article (2)'); $this->assertNoText('article');
// Do not show the block on empty behaviors. // Do not show the block on empty behaviors.
// Remove data from index. // Remove data from index.
......
...@@ -71,6 +71,7 @@ class LinksWidgetTest extends UnitTestCase { ...@@ -71,6 +71,7 @@ class LinksWidgetTest extends UnitTestCase {
public function testNoFilterResults() { public function testNoFilterResults() {
$facet = new Facet([], 'facet'); $facet = new Facet([], 'facet');
$facet->setResults($this->original_results); $facet->setResults($this->original_results);
$facet->set('widget_configs', ['show_numbers' => 1]);
$output = $this->widget->build($facet); $output = $this->widget->build($facet);
...@@ -92,6 +93,7 @@ class LinksWidgetTest extends UnitTestCase { ...@@ -92,6 +93,7 @@ class LinksWidgetTest extends UnitTestCase {
$facet = new Facet([], 'facet'); $facet = new Facet([], 'facet');
$facet->setResults($original_results); $facet->setResults($original_results);
$facet->set('widget_configs', ['show_numbers' => 1]);
$output = $this->widget->build($facet); $output = $this->widget->build($facet);
...@@ -114,6 +116,7 @@ class LinksWidgetTest extends UnitTestCase { ...@@ -114,6 +116,7 @@ class LinksWidgetTest extends UnitTestCase {
$facet = new Facet([], 'facet'); $facet = new Facet([], 'facet');
$facet->setResults($original_results); $facet->setResults($original_results);
$facet->set('widget_configs', ['show_numbers' => 1]);
$output = $this->widget->build($facet); $output = $this->widget->build($facet);
...@@ -126,5 +129,41 @@ class LinksWidgetTest extends UnitTestCase { ...@@ -126,5 +129,41 @@ class LinksWidgetTest extends UnitTestCase {
} }
} }
/**
* Test widget, make sure hiding and showing numbers works.
*/
public function testHideNumbers() {
$original_results = $this->original_results;
$original_results[1]->setActiveState(TRUE);
$facet = new Facet([], 'facet');
$facet->setResults($original_results);
$facet->set('widget_configs', ['show_numbers' => 0]);
$output = $this->widget->build($facet);
$this->assertInternalType('array', $output);
$this->assertCount(4, $output['#items']);
$expected_links = ['Llama', '(-) Badger', 'Duck', 'Alpaca'];
foreach ($expected_links as $index => $value) {
$this->assertEquals($value, $output['#items'][$index]);
}
// Enable the 'show_numbers' setting again to make sure that the switch
// between those settings works.
$facet->set('widget_configs', ['show_numbers' => 1]);
$output = $this->widget->build($facet);
$this->assertInternalType('array', $output);
$this->assertCount(4, $output['#items']);
$expected_links = ['Llama (10)', '(-) Badger (20)', 'Duck (15)', 'Alpaca (9)'];
foreach ($expected_links as $index => $value) {
$this->assertEquals($value, $output['#items'][$index]);
}
}
} }
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