isLatestRevision() false for new revisions on entity hook update

Created on 21 January 2022, almost 3 years ago
Updated 24 November 2022, about 2 years ago

Problem/Motivation

RevisionableInterface::isLatestRevision returns FALSE on hook_entity_update() and hook_ENTITY_TYPE_update() implementations for new revisions. It should return TRUE.

Steps to reproduce

Starting from a fresh 9.3 Drupal installation with standard profile, add a basic hook_ENTITY_TYPE_update() implementation like:

function <MY_CUSTOM_MODULE>_node_update(NodeInterface $node) {
  \Drupal::messenger()->addWarning($node->isLatestRevision() ? t('This is the latest revision') : t('This is NOT the latest revision'));
}

Create a new revision for an existing article. The "This is NOT the latest revision" message is shown.

Proposed resolution

TBD.

Remaining tasks

TBD.

User interface changes

N/A

API changes

N/A

Data model changes

N/A

Release notes snippet

TBD.

🐛 Bug report
Status

Active

Version

11.0 🔥

Component
Entity 

Last updated about 16 hours ago

Created by

🇪🇸Spain manuel.adan 🌌

Live updates comments and jobs are added and updated live.
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 almunnings Melbourne, 🇦🇺

    As a possible workaround short-term, I've had luck with this:

    function whatevers_node_update(NodeInterface $node) {
      /** @var \Drupal\node\NodeStorageInterface $storage */
      $storage = \Drupal::entityTypeManager()->getStorage('node');
      $storage->resetCache([$node->id()]);
    
      /** @var \Drupal\node\NodeInterface $reload */
      $reload = $storage->load($node->id());
      if (!$reload->isLatestRevision()) {
        return;
      }
    }
    
Production build 0.71.5 2024