Commit c21259b1 authored by mr.baileys's avatar mr.baileys
Browse files

Issue #2596367: Create the "display value" widget order processor

parent 9453ca9e
......@@ -9,6 +9,17 @@ facetapi.processor.plugin.count_widget_order:
type: string
label: sort order
facetapi.processor.plugin.display_value_widget_order:
type: mapping
label: 'Display value widget order'
mapping:
enabled:
type: boolean
label: 'Is the widget enabled?'
sort:
type: string
label: sort order
facetapi.processor.plugin.minimum_count:
type: mapping
label: 'Minimum count widget'
......
<?php
namespace Drupal\facetapi\Plugin\facetapi\processor;
use Drupal\facetapi\Processor\WidgetOrderPluginBase;
use Drupal\facetapi\Processor\WidgetOrderProcessorInterface;
use Drupal\facetapi\Result\Result;
/**
* @FacetApiProcessor(
* id = "display_value_widget_order",
* label = @Translation("Sort by display value"),
* description = @Translation("Sorts the widget results by display value."),
* stages = {
* "build" = 50
* }
* )
*/
class DisplayValueWidgetOrderProcessor extends WidgetOrderPluginBase implements WidgetOrderProcessorInterface {
/**
* {@inheritdoc}
*/
public function sortResults(array $results, $order = 'ASC') {
if ($order === 'ASC') {
usort($results, 'self::sortDisplayValueAsc');
}
else {
usort($results, 'self::sortDisplayValueDesc');
}
return $results;
}
protected static function sortDisplayValueAsc(Result $a, Result $b) {
return strnatcasecmp($a->getValue(), $b->getValue());
}
protected static function sortDisplayValueDesc(Result $a, Result $b) {
return strnatcasecmp($b->getValue(), $a->getValue());
}
}
<?php
/**
* @file
* Contains \Drupal\Tests\facetapi\Plugin\Processor\DisplayValueWidgetOrderProcessorTest.
*/
namespace Drupal\Tests\facetapi\Unit\Plugin\Processor;
use Drupal\facetapi\Plugin\facetapi\processor\DisplayValueWidgetOrderProcessor;
use Drupal\facetapi\Processor\WidgetOrderProcessorInterface;
use Drupal\facetapi\Result\Result;
use Drupal\Tests\UnitTestCase;
/**
* @group facetapi
*/
class DisplayValueWidgetOrderProcessorTest extends UnitTestCase {
/**
* The processor to be tested.
*
* @var WidgetOrderProcessorInterface
*/
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('thetans', 10),
new Result('xenu', 5),
new Result('Tom', 15),
new Result('Hubbard', 666),
new Result('FALSE', 1),
new Result('1977', 20),
new Result('2', 22),
];
$this->processor = new DisplayValueWidgetOrderProcessor([], 'display_value_widget_order', []);
}
/**
* Test sorting ascending.
*/
public function testAscending() {
$sorted_results = $this->processor->sortResults($this->original_results, 'ASC');
$expected_values = array('2', '1977', 'FALSE', 'Hubbard', 'thetans', 'Tom', 'xenu');
foreach ($expected_values as $index => $value) {
$this->assertEquals($value, $sorted_results[$index]->getValue());
}
}
/**
* Test sorting descending.
*/
public function testDescending() {
$sorted_results = $this->processor->sortResults($this->original_results, 'DESC');
$expected_values = array_reverse(array('2', '1977', 'FALSE', 'Hubbard', 'thetans', 'Tom', 'xenu'));
foreach ($expected_values as $index => $value) {
$this->assertEquals($value, $sorted_results[$index]->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