Hook presave does not set original correctly when editing non-default revision entities

Created on 2 March 2021, over 3 years ago
Updated 17 February 2024, 9 months ago

Problem/Motivation

\Drupal\Core\Entity\EntityStorageBase::doPreSave does not reload the 'original' node into $entity->original when workflows are enabled for the bundle and the entity being updated is not the 'Default Revision'.

Steps to reproduce

  1. Enable the Workflows module
  2. Add Basic Page as applying to the 'Editorial' workflow
  3. Create and move a basic page through the workflow to published, then set to draft
  4. Finally, edit the non-default revision (draft) basic page again and set it to ready for review
  5. In a presave hook, $entity->original will be set to the published revision instead of the draft revision.

Proposed resolution

\Drupal\Core\Entity\EntityStorageBase::doPreSave uses \Drupal\Core\Entity\EntityStorageBase::loadUnchanged instead of loading by the node's revision ID when the entity is not the default revision.

Workaround

Load the 'original' yourself in hook presave via the entity's revision ID.

πŸ› Bug report
Status

Needs work

Version

11.0 πŸ”₯

Component
EntityΒ  β†’

Last updated about 3 hours ago

Created by

πŸ‡ΊπŸ‡ΈUnited States mattsqd

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

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

  • πŸ‡·πŸ‡ΊRussia kala4ek πŸ‡·πŸ‡Ί Novosibirsk

    Increasing the priority of that ticket, because a lot of code do compare entities in the wrong way now.

    Also attached the actual changes from MR as patch, so it can be safely used with composer.

  • last update over 1 year ago
    30,341 pass
  • πŸ‡ΉπŸ‡·Turkey makbay

    Thanks for the MR and patch but this is still working incorrectly when the latest saved entity is the default version.

    Since the patch is checking if the latest version is default or not, in case the latest revision is default (published), the $entity->original again gets the previous default (published) revision instead of the latest revision.

    Steps to reproduce after patch

    1. Enable the Workflows module
    2. Add Basic Page as applying to the 'Editorial' workflow
    3. Create and move a basic page through the workflow to published, then set to draft
    4. Finally, edit the non-default revision (draft) basic page again and set it to ready for review
    5. In a presave hook, $entity->original will be set to the published revision instead of the draft revision.
    6. Publish the basic page again.
    7. Create a new non-default revision (draft). The patch is ok for now, $entity->original will be set to the published version.
    8. Create another new non-default revision (e.g in review) after the published one. The patch is still ok, $entity->original will be set to the draft version on the previous step.
    9. Now publish the basic page. The patch will not work here since there is !$entity->isDefaultRevision() check in the condition and $entity->original will be set to the previous published version on step 6.

    So, I think we need to remove !$entity->isDefaultRevision() check from the if block.

    I'll create another MR for this.

  • πŸ‡ΉπŸ‡·Turkey makbay

    makbay β†’ changed the visibility of the branch 9.2.x to hidden.

  • πŸ‡ΉπŸ‡·Turkey makbay

    makbay β†’ changed the visibility of the branch 11.x to hidden.

  • πŸ‡©πŸ‡ͺGermany tobiasb Berlin
Production build 0.71.5 2024