- Issue created by @albre40
- Merge request !19Issue 3519596: Sorting Accented Characters in Select Options → (Open) created by Unnamed author
When using VERF in a view to display a select with options, the default sorting mechanism (natcasesort()) used in the getValueOptions() method doesn't properly handle accented characters. As a result, entities with names containing accented characters (like é, è, ê, etc.) are not sorted correctly with their non-accented counterparts in alphabetical order. This causes usability issues for sites with multilingual content or content in languages that use accented characters.
Current sorting output example:
[...]
Elsa
Emilian
Emilie
Emmanuel
Emmanuelle
Emmy
Erica
Eric
[...]
Yves
Zeynab
Zhi
Élise
Éloïse
Éléonore
Émile
Émilie
Éric
Notice how 'Élise', 'Émilie' and 'Éric' appear after 'Zhi' instead of being grouped with their non-accented counterparts 'Elsa', 'Emilie' and 'Eric'.
Replace the natcasesort() function with PHP's Collator class from the Intl extension, which provides locale-aware string comparison. This allows for proper handling of accented characters in sorting.
// Get the current locale
$locale = $this->languageManager->getCurrentLanguage()->getId();
// Sort with proper collation
if (class_exists('Collator')) {
$collator = new \Collator($locale);
$collator->setStrength(\Collator::SECONDARY);
$collator->asort($this->valueOptions);
}
else {
// Fallback to natcasesort if Collator is not available
natcasesort($this->valueOptions);
}
Expected result with proper collation:
[...]
Elsa
Élise
Éléonore
Éloïse
Emilian
Emilie
Émile
Émilie
Emmanuel
Emmanuelle
Emmy
Erica
Eric
Éric
[...]
Yves
Zeynab
Zhi
Active
2.1
Code