Commit c9d5765d authored by mollux's avatar mollux Committed by borisson_

Issue #2598298 by mollux, borisson_: Create Facet processors - Do now show facet with only X items

parent b5b48802
<?php
/**
* @file
* Contains \Drupal\facetapi\Plugin\facetapi\processor.
*/
namespace Drupal\facetapi\Plugin\facetapi\processor;
use Drupal\Core\Form\FormStateInterface;
use Drupal\facetapi\FacetInterface;
use Drupal\facetapi\Processor\BuildProcessorInterface;
use Drupal\facetapi\Processor\ProcessorPluginBase;
use Drupal\facetapi\Result\Result;
/**
* Provides a minimum count processor..
*
* @FacetApiProcessor(
* id = "minimum_count",
* label = @Translation("Minimum count"),
* description = @Translation("Hide facets with less than x items."),
* stages = {
* "build" = 50
* }
* )
*/
class MinimumCountProcessor extends ProcessorPluginBase implements BuildProcessorInterface {
/**
* {@inheritdoc}
*/
public function build(FacetInterface $facet, array $results) {
$processor_configs = $facet->getProcessorConfigs();
$config = $processor_configs[$this->getPluginId()];
/** @var Result $result */
foreach ($results as $id => $result) {
if ($result->getCount() < $config['settings']['minimum_items']) {
unset($results[$id]);
}
}
return $results;
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state, FacetInterface $facet) {
$processor_configs = $facet->getProcessorConfigs();
$config = $processor_configs[$this->getPluginId()];
$build['minimum_items'] = array(
'#title' => 'Minimum items',
'#type' => 'number',
'#min' => 1,
'#default_value' => $config['settings']['minimum_items'],
'#description' => 'Hide block if the facet contains less than this number of results',
);
return $build;
}
}
<?php
/**
* @file
* Contains \Drupal\Tests\facetapi\Plugin\Processor\MinimumCountProcessorTest.
*/
namespace Drupal\Tests\facetapi\Unit\Plugin\Processor;
use Drupal\facetapi\Entity\Facet;
use Drupal\facetapi\Plugin\facetapi\processor\MinimumCountProcessor;
use Drupal\facetapi\Processor\BuildProcessorInterface;
use Drupal\facetapi\Result\Result;
use Drupal\Tests\UnitTestCase;
/**
* @group facetapi
*/
class MinimumCountProcessorTest extends UnitTestCase {
/**
* The processor to be tested.
*
* @var BuildProcessorInterface
*/
protected $processor;
/**
* An array containing the results before the processor has ran.
*
* @var \Drupal\facetapi\Result\Result[]
*/
protected $original_results;
/**
* Creates a new processor object for use in the tests.
*/
protected function setUp() {
parent::setUp();
$this->original_results = [
new Result('llama', 10),
new Result('badger', 5),
new Result('duck', 15),
];
$this->processor = new MinimumCountProcessor([], 'minimum_count', []);
}
/**
* Test filtering of results
*/
public function testFilterResults() {
$facet = new Facet([], 'facet');
$facet->setResults($this->original_results);
$facet->setProcessorConfigs([
'minimum_count' => [
'settings' => ['minimum_items' => 6]
]
]);
$sorted_results = $this->processor->build($facet, $this->original_results);
$this->assertCount(2, $sorted_results);
$this->assertEquals(10, $sorted_results[0]->getCount());
$this->assertEquals('llama', $sorted_results[0]->getValue());
$this->assertEquals(15, $sorted_results[2]->getCount());
$this->assertEquals('duck', $sorted_results[2]->getValue());
}
}
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