Cron throws errors when user entity lacks email address

Created on 4 June 2025, about 1 month ago

Problem/Motivation

When a site creates users programmatically, and does not populate the user's email field, the cron job throws the following error.

TypeError: Drupal\Component\Utility\Html::escape(): Argument #1 ($text) must be of type string, null given, called in /var/www/html/docroot/core/lib/Drupal/Component/Render/FormattableMarkup.php on line 238 in Drupal\Component\Utility\Html::escape() (line 431 of /var/www/html/docroot/core/lib/Drupal/Component/Utility/Html.php).

This is caused by calling $user->getEmail() in the $log_context.

$log_context = [
  '@username' => $user->getDisplayName(),
  '@mail' => $user->getEmail(),
];

Steps to reproduce

  1. Must have user entities created without an email address
  2. Execute cron after user is ready to disable

Proposed resolution

Since it is reasonable to expect a user entity would have an email, it seems like a simple null coalescing operator could solve the issue.

$log_context = [
  '@username' => $user->getDisplayName(),
  '@mail' => $user->getEmail() ?? 'No User Email',
];

Remaining tasks

Determine if the solution is valid.

User interface changes

N/A

API changes

N/A

Data model changes

N/A

πŸ› Bug report
Status

Active

Version

1.1

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States tregonia

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024