Closed (fixed)
Project:
Deactivate Users
Version:
1.1.2
Component:
Code
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Reporter:
Created:
4 Jun 2025 at 20:12 UTC
Updated:
11 Jan 2026 at 23:24 UTC
Jump to comment: Most recent
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
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
tregonia commentedAdded a null coalescing operator with generic string to prevent error.
Comment #6
john franklin commentedI've updated the patch to be more robust and create more accurate log messages. Attempting to email a user without an email address is now a
WARNINGlevel message to make it easier for site admins to notice when they have such users.I've also updated the tests to reflect the changes and add a couple more asserts for completeness.
@tregonia, can you check the new patch and report back if it works for you?
Comment #8
john franklin commentedNote: MR13 was closed because it was on the 1.x branch, not the issue branch. MR14 is the deactivate_users-3528482 branch and includes the patch from MR13.
Comment #9
john franklin commentedMarking "postponed" pending @tregonia's review of the patch.
Comment #10
john franklin commentedI'll merge this Sunday evening if I don't hear anything.
Comment #12
john franklin commentedMerged.