Commit a36a6778 authored by Tom Verhaeghe's avatar Tom Verhaeghe Committed by borisson_

Issue #2598304 by Tom Verhaeghe, borisson_, brentgees: Create Facet processors...

Issue #2598304 by Tom Verhaeghe, borisson_, brentgees: Create Facet processors - Show only deepest level items
parent 03e2f4a1
<?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 only shows deepest level items.
*
* @FacetsProcessor(
* id = "show_only_deepest_level_items_processor",
* label = @Translation("Show only deepest item levels"),
* description = @Translation("Only show items that have no children."),
* stages = {
* "build" = 40
* }
* )
*/
class ShowOnlyDeepestLevelItemsProcessor extends ProcessorPluginBase implements BuildProcessorInterface {
/**
* {@inheritdoc}
*/
public function build(FacetInterface $facet, array $results) {
/** @var \Drupal\facets\Result\ResultInterface $result */
foreach ($results as $id => $result) {
if (!empty($result->getChildren())) {
unset($results[$id]);
}
}
return $results;
}
}
<?php
namespace Drupal\Tests\facets\Unit\Plugin\processor;
use Drupal\facets\Entity\Facet;
use Drupal\facets\Plugin\facets\processor\ShowOnlyDeepestLevelItemsProcessor;
use Drupal\facets\Result\Result;
use Drupal\Tests\UnitTestCase;
/**
* Class ShowOnlyDeepestLevelItemsProcessorTest.
*
* @group facets
* @coversDefaultClass \Drupal\facets\Plugin\facets\processor\ShowOnlyDeepestLevelItemsProcessor
*/
class ShowOnlyDeepestLevelItemsProcessorTest extends UnitTestCase {
/**
* @var \Drupal\facets\Plugin\facets\processor\ShowOnlyDeepestLevelItemsProcessor
*/
protected $processor;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->processor = new ShowOnlyDeepestLevelItemsProcessor([], 'test', []);
}
/**
* Tests that only items without children survive.
*
* @covers ::build
*/
public function testRemoveItemsWithoutChildren() {
$facet = new Facet(['id' => 'llama'], 'facets_facet');
// Setup results.
$results = [
new Result($facet, 'a', 'A', 5),
new Result($facet, 'b', 'B', 2),
new Result($facet, 'c', 'C', 4),
];
$child = new Result($facet, 'a_1', 'A 1', 3);
$results[0]->setChildren([$child]);
// Execute the build method, so we can test the behavior.
$built_results = $this->processor->build($facet, $results);
// Sort to have a 0-indexed array.
sort($built_results);
// Check the output.
$this->assertCount(2, $built_results);
$this->assertSame('b', $built_results[0]->getRawValue());
$this->assertSame('c', $built_results[1]->getRawValue());
}
}
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