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

Command icon Show commands

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

tregonia created an issue. See original summary.

tregonia’s picture

Status: Active » Needs review

Added a null coalescing operator with generic string to prevent error.

john franklin made their first commit to this issue’s fork.

john franklin’s picture

I'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 WARNING level 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?

john franklin’s picture

Note: 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.

john franklin’s picture

Status: Needs review » Postponed (maintainer needs more info)

Marking "postponed" pending @tregonia's review of the patch.

john franklin’s picture

I'll merge this Sunday evening if I don't hear anything.

  • john franklin committed e9adc4ca on 1.x
    fix: #3528482 Cron throws errors when user entity lacks email address...
john franklin’s picture

Assigned: tregonia » Unassigned
Status: Postponed (maintainer needs more info) » Fixed

Merged.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.