Newer
Older
<?php
namespace Drupal\facets\Plugin\facets\processor;
Florian Weber
committed
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;
Florian Weber
committed
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* A processor that orders the results by display value.
*
* @FacetsProcessor(
* id = "display_value_widget_order",
* label = @Translation("Sort by display value"),
* description = @Translation("Sorts the widget results by display value."),
* stages = {
* "build" = 50
* }
* )
*/
Florian Weber
committed
class DisplayValueWidgetOrderProcessor extends WidgetOrderPluginBase implements WidgetOrderProcessorInterface, ContainerFactoryPluginInterface {
/**
Florian Weber
committed
* The transliteration service.
*
* @var \Drupal\Component\Transliteration\TransliterationInterface
*/
Florian Weber
committed
protected $transliteration;
Florian Weber
committed
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, TransliterationInterface $transliteration) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->transliteration = $transliteration;
}
Florian Weber
committed
* Creates an instance of the plugin.
Florian Weber
committed
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('transliteration')
);
}
Florian Weber
committed
* {@inheritdoc}
Jimmy Henderickx
committed
public function sortResults(Result $a, Result $b) {
$a = $this->transliteration->removeDiacritics($a->getDisplayValue());
$b = $this->transliteration->removeDiacritics($b->getDisplayValue());
if ($a == $b) {
return 0;
Florian Weber
committed
}
Jimmy Henderickx
committed
return strnatcasecmp($a, $b);