Passing null to mb_strlen is deprecated in template_preprocess_username

Created on 28 July 2022, about 2 years ago
Updated 30 November 2023, 9 months ago

Problem

mb_strlen() function on PHP 8.1 can only take string in PHP 8.1 version.I am seeing errors like this: Deprecated function: mb_strlen(): Passing null to parameter #1 ($string) of type string is deprecated in template_preprocess_username() (line 397 of core/modules/user/user.module).

πŸ› Bug report
Status

Postponed: needs info

Version

9.5

Component
User moduleΒ  β†’

Last updated 1 day ago

Created by

πŸ‡±πŸ‡ΉLithuania Edvinasas

Live updates comments and jobs are added and updated live.
  • PHP 8.1

    The issue particularly affects sites running on PHP version 8.1.0 or later.

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.

  • πŸ‡¦πŸ‡ΊAustralia ThomWilhelm Sydney

    I came across this same error while testing the PHP 8.1 upgrade, but it was actually caused by a custom module I wrote that reformatted the users display name by implementing hook_user_format_name_alter(). The problem was with the custom logic I wrote, the return value for the Anonymous user was returning NULL, triggering this error.

    To fix I needed to add a case for the anonymous user to not reformat their username:

    function my_module_user_format_name_alter(&$name, AccountInterface $account) {
      if ($account->isAnonymous()) {
        return;
      }
      // Existing custom logic.
    }
  • Status changed to Postponed: needs info over 1 year ago
  • πŸ‡¦πŸ‡ΊAustralia ThomWilhelm Sydney

    If this is to be re-opened, I feel we need steps to replicate to a vanilla Drupal install, as likely the issue is in custom code as previously mentioned.

  • πŸ‡ΈπŸ‡ͺSweden hansson192

    I got this exact error whilst trying to update Drupal to 9.5.9 and running phpUnit on our custom modules in a custom profile. Your patch did indeed solve our issues but whilst looking into the error I noticed that the user entity's getDisplayName defaults to user.settings value for anonymous, and said file was missing for us because of some changes that had been made to our testing process.

      public function getDisplayName() {
        $name = $this->getAccountName() ?: \Drupal::config('user.settings')->get('anonymous');
        \Drupal::moduleHandler()->alter('user_format_name', $name, $this);
        return $name;
      }

    So by making sure that user.settings.yml with the key anonymous and a value existed and were accessible the errors disappeared.

  • πŸ‡¬πŸ‡§United Kingdom jkdaza

    #17 πŸ› Passing null to mb_strlen is deprecated in template_preprocess_username Postponed: needs info really helped, thanks!

    We also experienced this issue and we tracked it down to one of the custom modules implementing `hook_user_format_name_alter` was returning NULL for anonymous users.

    I changed this:

    function MYMODULE_user_format_name_alter(&$name, AccountInterface $account) {
      $name = $account->getEmail();
    }
    

    into this:

    function MYMODULE_user_format_name_alter(&$name, AccountInterface $account) {
      if ($account->isAnonymous()) {
        return;
      }
      $name = $account->getEmail();
    }
    
Production build 0.71.5 2024