Improve hook_entity_presave documentation

Created on 7 January 2021, almost 4 years ago
Updated 16 March 2023, almost 2 years 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 2 days 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 almost 2 years ago
  • Status changed to Needs work almost 2 years 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.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia rassoni Bangalore

    Rashmisoni โ†’ made their first commit to this issueโ€™s 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 ๐Ÿ“Œ Define 'original' as property on the entity object Needs work where the property is defined rather than in the hook documentation.

Production build 0.71.5 2024