Skip to content
Snippets Groups Projects

Issue #3307968: Sort terms by weight in a multilingual environment

Open mschudders requested to merge issue/facets-3307968:3307968-sort-terms-by into 2.0.x
Files
2
@@ -5,6 +5,7 @@ namespace Drupal\facets\Plugin\facets\processor;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\UnchangingCacheableDependencyTrait;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\TypedData\ComplexDataDefinitionInterface;
use Drupal\Core\TypedData\DataReferenceDefinitionInterface;
@@ -36,6 +37,9 @@ class TermWeightWidgetOrderProcessor extends SortProcessorPluginBase implements
*/
protected $entityTypeManager;
/** @var \Drupal\Core\Language\LanguageManagerInterface $languageManager */
protected $languageManager;
/**
* Constructs a new object.
*
@@ -47,11 +51,14 @@ class TermWeightWidgetOrderProcessor extends SortProcessorPluginBase implements
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Language\LanguageManagerInterface
* The language manager.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $languageManager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->languageManager = $languageManager;
}
/**
@@ -62,7 +69,8 @@ class TermWeightWidgetOrderProcessor extends SortProcessorPluginBase implements
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_type.manager')
$container->get('entity_type.manager'),
$container->get('language_manager')
);
}
@@ -70,6 +78,8 @@ class TermWeightWidgetOrderProcessor extends SortProcessorPluginBase implements
* {@inheritdoc}
*/
public function sortResults(Result $a, Result $b) {
$currentLanguageId = $this->languageManager->getCurrentLanguage()->getId();
// Get the term weight once.
if (!isset($a->termWeight) || !isset($b->termWeight)) {
$ids = [];
@@ -88,13 +98,17 @@ class TermWeightWidgetOrderProcessor extends SortProcessorPluginBase implements
if (empty($entities[$a_raw])) {
return 0;
}
$a->termWeight = $entities[$a_raw]->getWeight();
/** @var \Drupal\taxonomy\Entity\Term $aTerm */
$aTerm = $entities[$a_raw];
$a->termWeight = $aTerm->getTranslation($currentLanguageId)->getWeight();
}
if (!isset($b->termWeight)) {
if (empty($entities[$b_raw])) {
return 0;
}
$b->termWeight = $entities[$b_raw]->getWeight();
/** @var \Drupal\taxonomy\Entity\Term $bTerm */
$bTerm = $entities[$b_raw];
$b->termWeight = $bTerm->getTranslation($currentLanguageId)->getWeight();
}
}
Loading