Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 294915

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)

$
0
0

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


Viewing all articles
Browse latest Browse all 294915

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>