- Issue created by @tregonia
- πΊπΈUnited States tregonia
Added a null coalescing operator with generic string to prevent error.
- Merge request !3Adds simple null coalescing operator to cron to provide a default string β (Open) created by tregonia
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(),
];
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',
];
Determine if the solution is valid.
N/A
N/A
N/A
Active
1.1
Code
Added a null coalescing operator with generic string to prevent error.