diff --git a/src/Plugin/facets/processor/DisplayValueWidgetOrderProcessor.php b/src/Plugin/facets/processor/DisplayValueWidgetOrderProcessor.php index badbf5c..e96b348 100644 --- a/src/Plugin/facets/processor/DisplayValueWidgetOrderProcessor.php +++ b/src/Plugin/facets/processor/DisplayValueWidgetOrderProcessor.php @@ -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,59 @@ use Drupal\facets\Result\Result; * } * ) */ -class DisplayValueWidgetOrderProcessor extends WidgetOrderPluginBase implements WidgetOrderProcessorInterface { +class DisplayValueWidgetOrderProcessor extends WidgetOrderPluginBase implements WidgetOrderProcessorInterface, ContainerFactoryPluginInterface { /** - * {@inheritdoc} + * @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; } } diff --git a/tests/src/Unit/Plugin/processor/DisplayValueWidgetOrderProcessorTest.php b/tests/src/Unit/Plugin/processor/DisplayValueWidgetOrderProcessorTest.php index 54812d4..296ef8c 100644 --- a/tests/src/Unit/Plugin/processor/DisplayValueWidgetOrderProcessorTest.php +++ b/tests/src/Unit/Plugin/processor/DisplayValueWidgetOrderProcessorTest.php @@ -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()); } /**