diff --git a/core/modules/content_moderation/src/ModerationInformation.php b/core/modules/content_moderation/src/ModerationInformation.php index 1d3fb0ad10..bb040d2ea0 100644 --- a/core/modules/content_moderation/src/ModerationInformation.php +++ b/core/modules/content_moderation/src/ModerationInformation.php @@ -86,26 +86,20 @@ public function shouldModerateEntitiesOfBundle(EntityTypeInterface $entity_type, * {@inheritdoc} */ public function getLatestRevision($entity_type_id, $entity_id) { - if ($latest_revision_id = $this->getLatestRevisionId($entity_type_id, $entity_id)) { - return $this->entityTypeManager->getStorage($entity_type_id)->loadRevision($latest_revision_id); - } + @trigger_error(__METHOD__ . ' is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use RevisionableStorageInterface::getLatestRevisionId and RevisionableStorageInterface::loadRevision instead. See https://www.drupal.org/node/3087295', E_USER_DEPRECATED); + /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */ + $storage = $this->entityTypeManager->getStorage($entity_type_id); + return $storage->loadRevision($storage->getLatestRevisionId($entity_id)); } /** * {@inheritdoc} */ public function getLatestRevisionId($entity_type_id, $entity_id) { + @trigger_error(__METHOD__ . ' is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use RevisionableStorageInterface::getLatestRevisionId instead. See https://www.drupal.org/node/3087295', E_USER_DEPRECATED); + /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */ if ($storage = $this->entityTypeManager->getStorage($entity_type_id)) { - $result = $storage->getQuery() - ->latestRevision() - ->condition($this->entityTypeManager->getDefinition($entity_type_id)->getKey('id'), $entity_id) - // No access check is performed here since this is an API function and - // should return the same ID regardless of the current user. - ->accessCheck(FALSE) - ->execute(); - if ($result) { - return key($result); - } + return $storage->getLatestRevisionId($entity_id); } } @@ -143,7 +137,8 @@ public function getAffectedRevisionTranslation(ContentEntityInterface $entity) { * {@inheritdoc} */ public function isLatestRevision(ContentEntityInterface $entity) { - return $entity->getRevisionId() == $this->getLatestRevisionId($entity->getEntityTypeId(), $entity->id()); + @trigger_error(__METHOD__ . ' is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use RevisionableInterface::isLatestRevision instead. See https://www.drupal.org/node/3087295', E_USER_DEPRECATED); + return $entity->isLatestRevision(); } /** @@ -171,7 +166,7 @@ public function hasPendingRevision(ContentEntityInterface $entity) { */ public function isLiveRevision(ContentEntityInterface $entity) { $workflow = $this->getWorkflowForEntity($entity); - return $this->isLatestRevision($entity) + return $entity->isLatestRevision() && $entity->isDefaultRevision() && $entity->moderation_state->value && $workflow->getTypePlugin()->getState($entity->moderation_state->value)->isPublishedState(); @@ -272,7 +267,8 @@ public function getOriginalState(ContentEntityInterface $entity) { * TRUE if this is the entity's first time being moderated, FALSE otherwise. */ protected function isFirstTimeModeration(ContentEntityInterface $entity) { - $original_entity = $this->getLatestRevision($entity->getEntityTypeId(), $entity->id()); + $storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId()); + $original_entity = $storage->loadRevision($storage->getLatestRevisionId($entity->id())); if ($original_entity) { $original_id = $original_entity->moderation_state; diff --git a/core/modules/content_moderation/src/ModerationInformationInterface.php b/core/modules/content_moderation/src/ModerationInformationInterface.php index 99c35dd350..ae915a9a60 100644 --- a/core/modules/content_moderation/src/ModerationInformationInterface.php +++ b/core/modules/content_moderation/src/ModerationInformationInterface.php @@ -69,6 +69,12 @@ public function isModeratedEntityType(EntityTypeInterface $entity_type); * @return \Drupal\Core\Entity\ContentEntityInterface|null * The latest entity revision or NULL, if the entity type / entity doesn't * exist. + * + * @deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use + * RevisionableStorageInterface::getLatestRevisionId and + * RevisionableStorageInterface::loadRevision instead. + * + * @see https://www.drupal.org/node/3087295 */ public function getLatestRevision($entity_type_id, $entity_id); @@ -83,6 +89,11 @@ public function getLatestRevision($entity_type_id, $entity_id); * @return int * The revision ID of the latest revision for the specified entity, or * NULL if there is no such entity. + * + * @deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use + * RevisionableStorageInterface::getLatestRevisionId instead. + * + * @see https://www.drupal.org/node/3087295 */ public function getLatestRevisionId($entity_type_id, $entity_id); @@ -120,6 +131,11 @@ public function getAffectedRevisionTranslation(ContentEntityInterface $entity); * @return bool * TRUE if the specified object is the latest revision of its entity, * FALSE otherwise. + * + * @deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use + * RevisionableInterface::isLatestRevision instead. + * + * @see https://www.drupal.org/node/3087295 */ public function isLatestRevision(ContentEntityInterface $entity); diff --git a/core/modules/content_moderation/tests/src/Kernel/ModerationInformationTest.php b/core/modules/content_moderation/tests/src/Kernel/ModerationInformationTest.php index e445c30490..a0274f6458 100644 --- a/core/modules/content_moderation/tests/src/Kernel/ModerationInformationTest.php +++ b/core/modules/content_moderation/tests/src/Kernel/ModerationInformationTest.php @@ -60,9 +60,8 @@ protected function setUp() { /** * @covers ::getDefaultRevisionId - * @covers ::getLatestRevisionId */ - public function testDefaultAndLatestRevisionId() { + public function testGetDefaultRevisionId() { $entity_test_rev = EntityTestRev::create([ 'name' => 'Default Revision', 'moderation_state' => 'published', @@ -77,6 +76,23 @@ public function testDefaultAndLatestRevisionId() { // revision ID. $default_revision_id = $this->moderationInformation->getDefaultRevisionId('entity_test_rev', $entity_test_rev->id()); $this->assertSame(1, $default_revision_id); + } + + /** + * @covers ::getLatestRevisionId + * @group legacy + * @expectedDeprecation Drupal\content_moderation\ModerationInformation::getLatestRevisionId is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use RevisionableStorageInterface::getLatestRevisionId instead. See https://www.drupal.org/node/3087295 + */ + public function testGetLatestRevisionId() { + $entity_test_rev = EntityTestRev::create([ + 'name' => 'Default Revision', + 'moderation_state' => 'published', + ]); + $entity_test_rev->save(); + + $entity_test_rev->name = 'Pending revision'; + $entity_test_rev->moderation_state = 'draft'; + $entity_test_rev->save(); // Check that moderation information service returns the correct latest // revision ID. diff --git a/core/modules/content_moderation/tests/src/Kernel/NodeAccessTest.php b/core/modules/content_moderation/tests/src/Kernel/NodeAccessTest.php index 3a2e0a9ed3..142400b109 100644 --- a/core/modules/content_moderation/tests/src/Kernel/NodeAccessTest.php +++ b/core/modules/content_moderation/tests/src/Kernel/NodeAccessTest.php @@ -67,9 +67,9 @@ protected function setUp() { } /** - * Tests for moderation information methods with node access. + * @covers \Drupal\content_moderation\ModerationInformation::getDefaultRevisionId */ - public function testModerationInformation() { + public function testGetDefaultRevisionId() { // Create an admin user. $user = $this->createUser([], NULL, TRUE); \Drupal::currentUser()->setAccount($user); @@ -77,12 +77,29 @@ public function testModerationInformation() { // Create a node. $node = $this->createNode(['type' => 'page']); $this->assertEquals($node->getRevisionId(), $this->moderationInformation->getDefaultRevisionId('node', $node->id())); - $this->assertEquals($node->getRevisionId(), $this->moderationInformation->getLatestRevisionId('node', $node->id())); // Create a non-admin user. $user = $this->createUser(); \Drupal::currentUser()->setAccount($user); $this->assertEquals($node->getRevisionId(), $this->moderationInformation->getDefaultRevisionId('node', $node->id())); + } + + /** + * @covers \Drupal\content_moderation\ModerationInformation::getLatestRevisionId + * @group legacy + */ + public function testGetLatestRevisionId() { + // Create an admin user. + $user = $this->createUser([], NULL, TRUE); + \Drupal::currentUser()->setAccount($user); + + // Create a node. + $node = $this->createNode(['type' => 'page']); + $this->assertEquals($node->getRevisionId(), $this->moderationInformation->getLatestRevisionId('node', $node->id())); + + // Create a non-admin user. + $user = $this->createUser(); + \Drupal::currentUser()->setAccount($user); $this->assertEquals($node->getRevisionId(), $this->moderationInformation->getLatestRevisionId('node', $node->id())); }