Problem/Motivation

user_update_notify_entity_update() reads the original user entity with $entity->original. Since Drupal 11.2, reading the original property is deprecated in favour of EntityInterface::getOriginal(), and the property is removed in Drupal 12 (see the change record).

  • On Drupal 11.2+ every profile update by a targeted user triggers a deprecation notice: "Getting the original property is deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. Use \Drupal\Core\Entity\EntityInterface::getOriginal() instead."
  • On Drupal 12, $entity->original is NULL, so UserUpdateNotifyFieldCompare::compare() throws a TypeError and saving the user fails with an EntityStorageException.

The module supports ^9.2 || ^10 || ^11, so getOriginal() can't simply replace the property: it doesn't exist before Drupal 11.2.

Steps to reproduce

  1. On Drupal 11.2 or later, enable the module, enable notifications, select a target role and at least one user field.
  2. Log in as a user with the target role and change the selected field on your own profile.
  3. The deprecation above is logged (or reported by PHPUnit when running the tests from #3631700: Add automated tests).
  4. On Drupal 12 (with the module and Token made compatible), the same steps fail with: UserUpdateNotifyFieldCompare::compare(): Argument #1 ($original) must be of type Drupal\Core\Entity\EntityInterface, null given.

Proposed resolution

Use getOriginal() when it exists, and fall back to the original property on older cores:

// EntityInterface::getOriginal() was added in Drupal 11.2, and the
// 'original' property was deprecated and is removed in Drupal 12.
$original = method_exists($entity, 'getOriginal') ? $entity->getOriginal() : $entity->original;

This keeps support for Drupal 9.2 to 11.1, removes the deprecation on 11.2+, and works on Drupal 12. Once the module drops support for cores older than 11.2, the fallback can be removed.

Verified locally with the tests from #3631700: Add automated tests:

  • Drupal 10.6 (fallback path): all tests pass.
  • Drupal 11.4: all tests pass, and the deprecation is gone.
  • Drupal 12.0-beta2 (with temporary compatibility changes for the module's info file and Token): all tests pass, including the 4 notification tests that previously failed.

Remaining tasks

  • Review the MR.

User interface changes

None.

API changes

None.

Data model changes

None.

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

beltofte created an issue. See original summary.

beltofte changed the visibility of the branch 3631702-replace-deprecated-entity-original to hidden.

beltofte’s picture

Status: Active » Needs work
beltofte’s picture

Status: Needs work » Postponed

Marking as postponed as it depends on other issues to be solved / committed first.
Wrong issue.

beltofte’s picture

Status: Postponed » Needs review

  • beltofte committed 2d2de31e on 1.0.x
    task: #3631702 Replace deprecated ->original with getOriginal()
    
beltofte’s picture

Status: Needs review » Fixed

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.