Commit c3a9617e authored by mr.baileys's avatar mr.baileys

#2599332: Converted minimum count processor to count limit processor, now...

#2599332: Converted minimum count processor to count limit processor, now supports maximum item count too.
parent 77c019bc
......@@ -30,10 +30,13 @@ facets.processor.plugin.active_widget_order:
type: string
label: sort order
facets.processor.plugin.minimum_count:
facets.processor.plugin.count_limit:
type: mapping
label: 'Minimum count widget'
label: 'Count limit widget'
mapping:
minimum_items:
type: integer
label: 'Mimimum amount of items to show.'
maximum_items:
type: integer
label: 'Maximum amount of items to show.'
......@@ -422,16 +422,16 @@ class FacetDisplayForm extends EntityForm {
// Iterate over all processors that have a form and are enabled.
foreach ($form['facet_settings'] as $processor_id => $processor_form) {
if (!empty($values['status'][$processor_id])) {
if (!empty($values['processors'][$processor_id])) {
$processor_form_state = new SubFormState($form_state, array('facet_settings', $processor_id, 'settings'));
$processors[$processor_id]->validateConfigurationForm($form['facet_settings'][$processor_id], $processor_form_state);
$processors[$processor_id]->validateConfigurationForm($form['facet_settings'][$processor_id], $processor_form_state, $facet);
}
}
// Iterate over all sorting processors that have a form and are enabled.
foreach ($form['facet_sorting'] as $processor_id => $processor_form) {
if (!empty($values['status'][$processor_id])) {
if (!empty($values['processors'][$processor_id])) {
$processor_form_state = new SubFormState($form_state, array('facet_sorting', $processor_id, 'settings'));
$processors[$processor_id]->validateConfigurationForm($form['facet_sorting'][$processor_id], $processor_form_state);
$processors[$processor_id]->validateConfigurationForm($form['facet_sorting'][$processor_id], $processor_form_state, $facet);
}
}
}
......
......@@ -12,29 +12,32 @@ use Drupal\facets\Processor\BuildProcessorInterface;
use Drupal\facets\Processor\ProcessorPluginBase;
/**
* Provides a minimum count processor..
* Provides a count limit processor.
*
* @FacetsProcessor(
* id = "minimum_count",
* label = @Translation("Minimum count"),
* description = @Translation("Hide facets with less than x items."),
* id = "count_limit",
* label = @Translation("Count limit"),
* description = @Translation("Show or hide facets with based on item count."),
* stages = {
* "build" = 50
* }
* )
*/
class MinimumCountProcessor extends ProcessorPluginBase implements BuildProcessorInterface {
class CountLimitProcessor extends ProcessorPluginBase implements BuildProcessorInterface {
/**
* {@inheritdoc}
*/
public function build(FacetInterface $facet, array $results) {
$processors = $facet->getProcessors();
$config = $processors[$this->getPluginId()];
$config = $processors[$this->getPluginId()]->getConfiguration();
$min_count = $config['minimum_items'];
$max_count = $config['maximum_items'];
/** @var \Drupal\facets\Result\Result $result */
foreach ($results as $id => $result) {
if ($result->getCount() < $config->getConfiguration()['minimum_items']) {
if (($min_count && $result->getCount() < $min_count) ||
($max_count && $result->getCount() > $max_count)) {
unset($results[$id]);
}
}
......@@ -57,14 +60,34 @@ class MinimumCountProcessor extends ProcessorPluginBase implements BuildProcesso
'#description' => $this->t('Hide block if the facet contains less than this number of results.'),
);
$max_default_value = !is_null($config) ? $config->getConfiguration()['maximum_items'] : $this->defaultConfiguration()['maximum_items'];
$build['maximum_items'] = array(
'#title' => $this->t('Maximum items'),
'#type' => 'number',
'#min' => 1,
'#default_value' => $max_default_value ? $max_default_value : '',
'#description' => $this->t('Hide block if the facet contains more than this number of results.'),
);
return $build;
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array $form, FormStateInterface $form_state, FacetInterface $facet) {
return parent::validateConfigurationForm($form, $form_state, $facet); // TODO: Change the autogenerated stub
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return array('minimum_items' => 1);
return array(
'minimum_items' => 1,
'maximum_items' => 0,
);
}
}
......@@ -2,13 +2,13 @@
/**
* @file
* Contains \Drupal\Tests\facets\Plugin\Processor\MinimumCountProcessorTest.
* Contains \Drupal\Tests\facets\Plugin\Processor\CountLimitProcessorTest.
*/
namespace Drupal\Tests\facets\Unit\Plugin\Processor;
use Drupal\facets\Entity\Facet;
use Drupal\facets\Plugin\facets\processor\MinimumCountProcessor;
use Drupal\facets\Plugin\facets\processor\CountLimitProcessor;
use Drupal\facets\Result\Result;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
......@@ -16,7 +16,7 @@ use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
* @group facets
*/
class MinimumCountProcessorTest extends UnitTestCase {
class CountLimitProcessorTest extends UnitTestCase {
/**
* The processor to be tested.
......@@ -44,13 +44,13 @@ class MinimumCountProcessorTest extends UnitTestCase {
new Result('duck', 'duck', 15),
];
$processor_id = 'minimum_count';
$this->processor = new MinimumCountProcessor([], $processor_id, []);
$processor_id = 'count_limit';
$this->processor = new CountLimitProcessor([], $processor_id, []);
$processorDefinitions = [
$processor_id => [
'id' => $processor_id,
'class' => 'Drupal\facets\Plugin\facets\processor\MinimumCountProcessor',
'class' => 'Drupal\facets\Plugin\facets\processor\CountLimitProcessor',
],
];
......@@ -74,11 +74,10 @@ class MinimumCountProcessorTest extends UnitTestCase {
* Test no filtering happens.
*/
public function testNoFilter() {
$facet = new Facet([], 'facet');
$facet->setResults($this->original_results);
$facet->setOption('processors', [
'minimum_count' => [
'count_limit' => [
'settings' => ['minimum_items' => 4],
],
]);
......@@ -96,11 +95,10 @@ class MinimumCountProcessorTest extends UnitTestCase {
* Test no filtering happens.
*/
public function testMinEqualsValue() {
$facet = new Facet([], 'facet');
$facet->setResults($this->original_results);
$facet->setOption('processors', [
'minimum_count' => [
'count_limit' => [
'settings' => ['minimum_items' => 5],
],
]);
......@@ -115,15 +113,38 @@ class MinimumCountProcessorTest extends UnitTestCase {
$this->assertEquals('duck', $sorted_results[2]->getDisplayValue());
}
/**
* Test between minimum and maximum values.
*/
public function testBetweenMinAndMaxValue() {
$facet = new Facet([], 'facet');
$facet->setResults($this->original_results);
$facet->setOption('processors', [
'count_limit' => [],
]);
$this->processor->setConfiguration(['minimum_items' => 6, 'maximum_items' => 14]);
$sorted_results = $this->processor->build($facet, $this->original_results);
$this->assertCount(1, $sorted_results);
$this->assertEquals('llama', $sorted_results[0]->getDisplayValue());
$this->processor->setConfiguration(['minimum_items' => 60, 'maximum_items' => 140]);
$sorted_results = $this->processor->build($facet, $this->original_results);
$this->assertCount(0, $sorted_results);
$this->processor->setConfiguration(['minimum_items' => 1, 'maximum_items' => 10]);
$sorted_results = $this->processor->build($facet, $this->original_results);
$this->assertCount(2, $sorted_results);
}
/**
* Test filtering of results.
*/
public function testFilterResults() {
$facet = new Facet([], 'facet');
$facet->setResults($this->original_results);
$facet->setOption('processors', [
'minimum_count' => [
'count_limit' => [
'settings' => ['minimum_items' => 8],
],
]);
......
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