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
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
Comment #6
larowlanComment #9
royalpinto007This 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.
Comment #10
royalpinto007Comment #11
smustgrave commentedChange definitely seems better then before.
But this phrase
Feel if we are going to mention that we should mention what those are or least where someone would go to look.
Comment #13
mstrelan commentedIs this specific to
hook_entity_presaveor should we update it forhook_entity_updateas well? Also notehook_ENTITY_TYPE_presaveandhook_ENTITY_TYPE_updatealso 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.
Comment #18
mdranove commentedComment #19
mdranove commentedComment #20
smustgrave commentedThink #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.
Comment #21
mdranove commentedYeah, 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.
Comment #22
mdranove commentedThe 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.