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)

Created on 12 December 2023, 12 months ago
Updated 2 March 2024, 9 months ago

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

  1. Language module must be enabled
  2. Refresh any page implementing LanguageInterface

Proposed resolution

Cast the parameters passed to strnatcasecmp to string.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Postponed: needs info

Version

11.0 🔥

Component
Language system 

Last updated 1 day ago

  • Maintained by
  • 🇩🇪Germany @sun
Created by

🇮🇱Israel albertvankiel Tel Aviv-Yafo

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024