API page: https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!entity.api...

A common use case for hook_entity_update(), hook_entity_presave(), and the associated hook_ENTITY_TYPE_*() hooks is acting if a certain thing has changed.

For that, you need to be able to get hold of the original copy of the entity. It's available in $entity->original. This should be documented.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

joachim created an issue. See original summary.

er.pushpinderrana’s picture

Status: Active » Needs review
FileSize
705 bytes

Please review.

jhodgdon’s picture

Status: Needs review » Needs work

Thanks -- Good idea to document this! Patch needs a small fix:

+++ b/core/lib/Drupal/Core/Entity/entity.api.php
@@ -964,7 +964,8 @@ function hook_ENTITY_TYPE_insert(Drupal\Core\Entity\EntityInterface $entity) {
+ * implementations may not alter the stored entity data. Gets the original

Gets => Get

er.pushpinderrana’s picture

Status: Needs work » Needs review
FileSize
704 bytes

Thank you! As recommended, changed Gets with Get.

Please note as this is just a minor change - NOT attaching interdiff file.

jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community

Thanks! Yeah, there is not any reason to make an interdiff file on a patch that is only 2 lines long anyway. ;)

xjm’s picture

Version: 8.0.x-dev » 8.1.x-dev
Issue tags: +rc eligible

8.0.x is now frozen for its final patch release. This issue is rc eligible for 8.1.x as a docs improvement.

joachim’s picture

Status: Reviewed & tested by the community » Needs work

Shouldn't this be documented in the other similar hooks too?

jhodgdon’s picture

Which other hooks besides when you are updating would have $entity->orginal? It seems like it is just for updates??

joachim’s picture

hook_entity_presave() has it -- but only if it's an existing entity being updated, not on creation -- and also the corresponding hook_ENTITY_TYPE_update() and hook_ENTITY_TYPE_presave().

elakiyasamuel’s picture

Assigned: Unassigned » elakiyasamuel
elakiyasamuel’s picture

Version: 8.1.x-dev » 8.2.x-dev
elakiyasamuel’s picture

Status: Needs work » Needs review
FileSize
1.33 KB
720 bytes

Please review.

elakiyasamuel’s picture

Assigned: elakiyasamuel » Unassigned
jhodgdon’s picture

Status: Needs review » Needs work

Thanks for the new patch! It needs a bit of work though...

  1. +++ b/core/lib/Drupal/Core/Entity/entity.api.php
    @@ -882,7 +882,8 @@ function hook_ENTITY_TYPE_storage_load(array $entities) {
      * Act on an entity before it is created or updated.
    - *
    + * Get the original entity object from $entity->original before entity update.
    + * ¶
    

    We want to leave the blank line that was here.

    Also, the last line in this group has an extra space at the end of it. You should be able to configure your programming editor to either highlight or remove spaces at the ends of lines.

    And... I am not sure this added sentence makes sense, without the context that is there in the update hook? Maybe it just needs to start with "You can get" instead of just "Get" and ... I don't think "before entity update" at the end is necessary?

  2. +++ b/core/lib/Drupal/Core/Entity/entity.api.php
    @@ -898,7 +899,8 @@ function hook_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {
      * Act on a specific type of entity before it is created or updated.
    - *
    + * Get the original entity object from $entity->original before entity update.
    + * ¶
    

    Same comments as for the previous block.

therealssj’s picture

Changes in accordance with #14
I am still not sure about 'You can get' instead of just 'Get'.
The latter does somewhat feel like an order but had to read quite a few times to feel so.
Didn't change it at the last place as reading it in continuation with other text made it feel right.

Please review.

therealssj’s picture

Status: Needs work » Needs review
joachim’s picture

Status: Needs review » Needs work
+++ b/core/lib/Drupal/Core/Entity/entity.api.php
@@ -883,6 +883,8 @@ function hook_ENTITY_TYPE_storage_load(array $entities) {
+ * You can get the original entity object from $entity->original before entity update.
+ *

@@ -899,6 +901,8 @@ function hook_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {
+ * You can get the original entity object from $entity->original before entity update.
+ *

Needs wrapping to 80 lines.

Also, we have 3 hooks being changed here, but there are 4 in question:

  • hook_entity_update()
  • hook_entity_presave()
  • hook_ENTITY_TYPE_update()
  • hook_ENTITY_TYPE_presave()

And we should possibly qualify what we're saying for the presave() hooks with something like "when this is an update", as if you try to access properties on $entity->original in hook_entity_presave() when creating an entity, it crashes (as I found out yesterday :D)

imalabya’s picture

Status: Needs work » Needs review
FileSize
1.85 KB
1.55 KB

Added a patch for the above comment.

jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community

This latest patch looks good to me, and seems to cover the 4 hooks. Addresses #17 as well. Thanks!

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed cdb3bc2 and pushed to 8.1.x and 8.2.x. Thanks!

  • alexpott committed 0ceb441 on 8.2.x
    Issue #2700335 by er.pushpinderrana, elakiyasamuel, malavya, therealssj...

  • alexpott committed cdb3bc2 on 8.1.x
    Issue #2700335 by er.pushpinderrana, elakiyasamuel, malavya, therealssj...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.