W3cubDocs

/Drupal 8

public static function Language::sort

public static Language::sort(&$languages)

Sort language objects.

Parameters

\Drupal\Core\Language\LanguageInterface[] $languages: The array of language objects keyed by langcode.

File

core/lib/Drupal/Core/Language/Language.php, line 146

Class

Language
An object containing the information for an interface language.

Namespace

Drupal\Core\Language

Code

public static function sort(&$languages) {
  uasort($languages, function(LanguageInterface $a, LanguageInterface $b) {
    $a_weight = $a->getWeight();
    $b_weight = $b->getWeight();
    if ($a_weight == $b_weight) {
      $a_name = $a->getName();
      $b_name = $b->getName();
      // If either name is a TranslatableMarkup object it can not be converted
      // to a string. This is because translation requires a sorted list of
      // languages thereby causing an infinite loop. Determine the order based
      // on ID if this is the case.
      if ($a_name instanceof TranslatableMarkup || $b_name instanceof TranslatableMarkup) {
        $a_name = $a->getId();
        $b_name = $b->getId();
      }
      return strnatcasecmp($a_name, $b_name);
    }
    return ($a_weight < $b_weight) ? -1 : 1;
  });
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Language!Language.php/function/Language::sort/8.1.x