Problem/Motivation

Doc comments for hook_entity_presave currently read

You can get the original entity object from $entity->original when it is an update of the entity.

This is ambiguous in a multi-language context and could be improved.

Steps to reproduce

In the following scenario, I was expecting $node->original to be the original version of the entity before it was updated, but it is not. It's actually the default translation of the entity (eg. the same as you'd get from doing a Node::load($nid).

* Add a new node in the [site] default language
* Add and save a translation of it
* Edit and save the translation

In hook_entity_presave(), $node->original is actually not the original translation that was edited, but is seemingly just the result of Node::load() on the ID of the edited node.

Proposed resolution

Update the hook_entity_presave() documentation to add information about how to access the original version of the entity in a multilingual setup.

While this could be $entity->original, it might be better to use $entity->getUntranslated() depending on the site's default translation configuration.

Issue fork drupal-3191463

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

nicrodgers created an issue. See original summary.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

larowlan’s picture

royalpinto007 made their first commit to this issue’s fork.

royalpinto007’s picture

This commit updates the documentation for the Drupal API page to provide more clarity on accessing the original entity object in a multi-language context. It also suggests alternative methods for retrieving the original translation if $entity->original does not refer to the expected translation. This should help improve understanding for developers working with Drupal in a multi-language context.

royalpinto007’s picture

Status: Active » Needs review
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

Change definitely seems better then before.

But this phrase

* In such cases, you can use other available methods
* to retrieve the original translation.

Feel if we are going to mention that we should mention what those are or least where someone would go to look.

Rashmisoni made their first commit to this issue’s fork.

mstrelan’s picture

Is this specific to hook_entity_presave or should we update it for hook_entity_update as well? Also note hook_ENTITY_TYPE_presave and hook_ENTITY_TYPE_update also mention $entity->original.

I'm also wondering if it's better to address this in #2839195: Add a method to access the original property where the property is defined rather than in the hook documentation.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mdranove made their first commit to this issue’s fork.

mdranove changed the visibility of the branch 3191463-improve-hookentitypresave-documentation to hidden.

mdranove’s picture

Issue summary: View changes
mdranove’s picture

Status: Needs work » Needs review
smustgrave’s picture

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

Think #13 needs to be answered. Wondering if this is needed anymore now that the issue mentioned has landed. If still needed don’t think the previous description is fully correct with the latest changes.

And summary isn’t using the correct summary template.

mdranove’s picture

Yeah, good point, because of https://www.drupal.org/node/3295826 now you can get the original entity by doing $entity->getOriginal(). I see this in the doc comment for hook_ENTITY_TYPE_presave, not sure if we want to also add it to hook_entity_presave.

mdranove’s picture

Issue summary: View changes
Status: Postponed (maintainer needs more info) » Closed (outdated)

The documentation on the api page and in core/lib/Drupal/Core/Entity/entity.api.php have been updated to recommend $entity->getOriginal() instead of $entity->original, which makes this issue no longer relevant.

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

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

Maintainers, please credit people who helped resolve this issue.