diff --git a/src/Plugin/facets/processor/DisplayValueWidgetOrderProcessor.php b/src/Plugin/facets/processor/DisplayValueWidgetOrderProcessor.php index 638ad73..e2ef5d0 100644 --- a/src/Plugin/facets/processor/DisplayValueWidgetOrderProcessor.php +++ b/src/Plugin/facets/processor/DisplayValueWidgetOrderProcessor.php @@ -7,9 +7,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. @@ -23,34 +26,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; } }