- π¦πΊ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 5:51am 18 April 2023 - π¦πΊ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(); }