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.

Comments

manuel.adan created an issue. See original summary.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

rp7’s picture

I can confirm that this issue exists. Anyone has a workaround on how to figure out whether the entity in hook_entity_update() is the latest revision?

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

almunnings’s picture

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;
  }
}

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.