Commit 598bd146 authored by webflo's avatar webflo Committed by borisson_
Browse files

Issue #2704855 by webflo, borisson_: Remove diacritics when sorting by display value

parent f48a9c35
......@@ -2,9 +2,12 @@
namespace Drupal\facets\Plugin\facets\processor;
use Drupal\Component\Transliteration\TransliterationInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\facets\Processor\WidgetOrderPluginBase;
use Drupal\facets\Processor\WidgetOrderProcessorInterface;
use Drupal\facets\Result\Result;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* A processor that orders the results by display value.
......@@ -18,34 +21,61 @@ use Drupal\facets\Result\Result;
* }
* )
*/
class DisplayValueWidgetOrderProcessor extends WidgetOrderPluginBase implements WidgetOrderProcessorInterface {
class DisplayValueWidgetOrderProcessor extends WidgetOrderPluginBase implements WidgetOrderProcessorInterface, ContainerFactoryPluginInterface {
/**
* {@inheritdoc}
* The transliteration service.
*
* @var \Drupal\Component\Transliteration\TransliterationInterface
*/
public function sortResults(array $results, $order = 'ASC') {
if ($order === 'ASC') {
usort($results, 'self::sortDisplayValueAsc');
}
else {
usort($results, 'self::sortDisplayValueDesc');
}
protected $transliteration;
return $results;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, TransliterationInterface $transliteration) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->transliteration = $transliteration;
}
/**
* Sorts ascending.
* Creates an instance of the plugin.
*/
protected static function sortDisplayValueAsc(Result $a, Result $b) {
return strnatcasecmp($a->getDisplayValue(), $b->getDisplayValue());
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('transliteration')
);
}
/**
* Sorts descending.
* {@inheritdoc}
*/
protected static function sortDisplayValueDesc(Result $a, Result $b) {
return strnatcasecmp($b->getDisplayValue(), $a->getDisplayValue());
public function sortResults(array $results, $order = 'ASC') {
$transliteration = $this->transliteration;
if ($order === 'ASC') {
// Sorts ascending.
usort($results, function (Result $a, Result $b) use ($transliteration) {
return strnatcasecmp(
$transliteration->removeDiacritics($a->getDisplayValue()),
$transliteration->removeDiacritics($b->getDisplayValue())
);
});
}
else {
// Sorts descending.
usort($results, function (Result $a, Result $b) use ($transliteration) {
return strnatcasecmp(
$transliteration->removeDiacritics($b->getDisplayValue()),
$transliteration->removeDiacritics($a->getDisplayValue())
);
});
}
return $results;
}
}
......@@ -2,6 +2,7 @@
namespace Drupal\Tests\facets\Unit\Plugin\processor;
use Drupal\Component\Transliteration\TransliterationInterface;
use Drupal\facets\Entity\Facet;
use Drupal\facets\Plugin\facets\processor\DisplayValueWidgetOrderProcessor;
use Drupal\facets\Processor\ProcessorPluginManager;
......@@ -46,7 +47,13 @@ class DisplayValueWidgetOrderProcessorTest extends UnitTestCase {
new Result('2', '2', 22),
];
$this->processor = new DisplayValueWidgetOrderProcessor([], 'display_value_widget_order', []);
$transliteration = $this
->getMockBuilder(TransliterationInterface::class)
->getMock();
$transliteration->method('removeDiacritics')->willReturnCallback(function ($value) {
return str_replace('Ä', 'A', $value);
});
$this->processor = new DisplayValueWidgetOrderProcessor([], 'display_value_widget_order', [], $transliteration);
}
/**
......@@ -94,12 +101,14 @@ class DisplayValueWidgetOrderProcessorTest extends UnitTestCase {
$original = [
new Result('bb_test', 'Test AA', 10),
new Result('aa_test', 'Test BB', 10),
new Result('ab_test', 'Test ÄB', 10),
];
$sorted_results = $this->processor->sortResults($original, 'DESC');
$this->assertEquals('Test BB', $sorted_results[0]->getDisplayValue());
$this->assertEquals('Test AA', $sorted_results[1]->getDisplayValue());
$this->assertEquals('Test ÄB', $sorted_results[1]->getDisplayValue());
$this->assertEquals('Test AA', $sorted_results[2]->getDisplayValue());
}
/**
......
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