Problem/Motivation
Deprecated function: strnatcasecmp(): Passing null to parameter #2 ($string2) of type string is deprecated in Drupal\Core\Language\Language::Drupal\Core\Language\{closure}() (line 161 of /app/docroot/core/lib/Drupal/Core/Language/Language.php)
When using PHP 8.1 this warning appears in the watchdog logs..
It happens in this function in /core/lib/Drupal/Core/Language/Language.php
/**
* Sort language objects.
*
* @param \Drupal\Core\Language\LanguageInterface[] $languages
* The array of language objects keyed by langcode.
*/
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((string) $a_name, (string) $b_name);
}
return $a_weight <=> $b_weight;
});
}
The "strnatcasecmp" function is expecting strings to be passed as parameters, and it will throw this warning when the parameters are NULL.
Steps to reproduce
- Language module must be enabled
- Refresh any page implementing LanguageInterface
Proposed resolution
Cast the parameters passed to strnatcasecmp to string.