Improve hook_entity_presave documentation

Created on 7 January 2021, over 3 years ago
Updated 16 March 2023, over 1 year ago

API page: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21...

Currently says:

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.

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.

(If this is not the expected behaviour, I will change this to be a code bug rather than a documentation improvement!)

🐛 Bug report
Status

Needs work

Version

9.5

Component
Documentation 

Last updated about 19 hours ago

No maintainer
Created by

🇬🇧United Kingdom nicrodgers Monmouthshire, UK

Live updates comments and jobs are added and updated live.
  • Novice

    It would make a good project for someone who is new to the Drupal contribution process. It's preferred over Newbie.

  • Documentation

    Primarily changes documentation, not code. For Drupal core issues, select the Documentation component instead of using this tag. In general, component selection is preferred over tag selection.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇦🇺Australia larowlan 🇦🇺🏝.au GMT+10
  • First commit to issue fork.
  • @royalpinto007 opened merge request.
  • 🇮🇳India royalpinto007

    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.

  • Status changed to Needs review over 1 year ago
  • Status changed to Needs work over 1 year ago
  • 🇺🇸United States smustgrave

    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.

  • First commit to issue fork.
  • 🇦🇺Australia mstrelan

    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 📌 Add a method to access the original property Needs work where the property is defined rather than in the hook documentation.

Production build 0.69.0 2024