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, 7 months ago
Updated 2 March 2024, 4 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 about 21 hours ago

  • Maintained by
  • πŸ‡©πŸ‡ͺGermany @sun
Created by

πŸ‡³πŸ‡±Netherlands albertvankiel

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.69.0 2024