diff --git a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php index ac5da7a..53fe964 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php +++ b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php @@ -17,6 +17,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Session\AccountInterface; +use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\user\EntityOwnerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -269,13 +270,13 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta // Note that the count here is after one element has been popped off, // so we're detecting if the list originally had additional elements. if (count($list) > 0 ) { - $options[$machine_name] = (string) $this->t('@label|… [@machine_name]', $arguments); + $options[$machine_name] = $this->t('@label|… [@machine_name]', $arguments); } else { - $options[$machine_name] = (string) $this->t('@label [@machine_name]', $arguments); + $options[$machine_name] = $this->t('@label [@machine_name]', $arguments); } } - asort($options); + uasort($options, DefaultSelection::compareTranslatables); $form['sort']['field'] = [ '#type' => 'select', @@ -523,4 +524,22 @@ protected function reAlterQuery(AlterableInterface $query, $tag, $base_table) { $query->alterMetaData = $old_metadata; } + /** + * Calculates the sort order of translatable markup, without changing them. + * + * @param \Drupal\Core\StringTranslation\TranslatableMarkup $a + * First translatable markup object to compare. + * @param \Drupal\Core\StringTranslation\TranslatableMarkup $b + * Second translatable markup object to compare. + * + * @return int + * A number (respectively) greater than zero, zero, or less than zero if + * $a is greater than, equal to, or less than $b (case sensitive). + */ + public static function compareTranslatables(TranslatableMarkup $a, TranslatableMarkup $b) { + $a = (string) clone $a; + $b = (string) clone $b; + return strcmp($a, $b); + } + }