diff --git a/core/modules/content_moderation/src/Entity/ContentModerationState.php b/core/modules/content_moderation/src/Entity/ContentModerationState.php index d60dad7..2256147 100644 --- a/core/modules/content_moderation/src/Entity/ContentModerationState.php +++ b/core/modules/content_moderation/src/Entity/ContentModerationState.php @@ -156,9 +156,16 @@ public static function getCurrentUserId() { * {@inheritdoc} */ public function save() { - $related_entity = \Drupal::entityTypeManager() - ->getStorage($this->content_entity_type_id->value) - ->loadRevision($this->content_entity_revision_id->value); + $related_entity_type = $this->entityTypeManager()->getDefinition($this->content_entity_type_id->value); + $related_entity_storage = \Drupal::entityTypeManager()->getStorage($this->content_entity_type_id->value); + + if (!$related_entity_type->isRevisionable()) { + $related_entity = $related_entity_storage->load($this->content_entity_id->value); + } + else { + $related_entity = $related_entity_storage->loadRevision($this->content_entity_revision_id->value); + } + if ($related_entity instanceof TranslatableInterface) { $related_entity = $related_entity->getTranslation($this->activeLangcode); } diff --git a/core/modules/content_moderation/src/Entity/Handler/ModerationHandler.php b/core/modules/content_moderation/src/Entity/Handler/ModerationHandler.php index 62e504e..73e65fe 100644 --- a/core/modules/content_moderation/src/Entity/Handler/ModerationHandler.php +++ b/core/modules/content_moderation/src/Entity/Handler/ModerationHandler.php @@ -30,9 +30,11 @@ public static function createInstance(ContainerInterface $container, EntityTypeI * {@inheritdoc} */ public function onPresave(ContentEntityInterface $entity, $default_revision, $published_state) { - // This is probably not necessary if configuration is setup correctly. - $entity->setNewRevision(TRUE); - $entity->isDefaultRevision($default_revision); + if ($entity->getEntityType()->isRevisionable()) { + // This is probably not necessary if configuration is setup correctly. + $entity->setNewRevision(TRUE); + $entity->isDefaultRevision($default_revision); + } // Update publishing status if it can be updated and if it needs updating. if (($entity instanceof EntityPublishedInterface) && $entity->isPublished() !== $published_state) { diff --git a/core/modules/content_moderation/src/EntityOperations.php b/core/modules/content_moderation/src/EntityOperations.php index af87f6f..0da8844 100644 --- a/core/modules/content_moderation/src/EntityOperations.php +++ b/core/modules/content_moderation/src/EntityOperations.php @@ -127,7 +127,9 @@ public function entityPresave(EntityInterface $entity) { public function entityInsert(EntityInterface $entity) { if ($this->moderationInfo->isModeratedEntity($entity)) { $this->updateOrCreateFromEntity($entity); - $this->setLatestRevision($entity); + if ($entity->getEntityType()->isRevisionable()) { + $this->setLatestRevision($entity); + } } } @@ -140,10 +142,7 @@ public function entityInsert(EntityInterface $entity) { * @see hook_entity_update() */ public function entityUpdate(EntityInterface $entity) { - if ($this->moderationInfo->isModeratedEntity($entity)) { - $this->updateOrCreateFromEntity($entity); - $this->setLatestRevision($entity); - } + $this->entityInsert($entity); } /** @@ -163,7 +162,7 @@ protected function updateOrCreateFromEntity(EntityInterface $entity) { // @todo what if $entity->moderation_state is null at this point? $entity_type_id = $entity->getEntityTypeId(); $entity_id = $entity->id(); - $entity_revision_id = $entity->getRevisionId(); + $entity_revision_id = $entity->getEntityType()->isRevisionable() ? $entity->getRevisionId() : $entity->id(); $storage = $this->entityTypeManager->getStorage('content_moderation_state'); $entities = $storage->loadByProperties([ diff --git a/core/modules/content_moderation/src/EntityTypeInfo.php b/core/modules/content_moderation/src/EntityTypeInfo.php index f11e471..9745c1e 100644 --- a/core/modules/content_moderation/src/EntityTypeInfo.php +++ b/core/modules/content_moderation/src/EntityTypeInfo.php @@ -125,7 +125,7 @@ public static function create(ContainerInterface $container) { public function entityTypeAlter(array &$entity_types) { foreach ($entity_types as $entity_type_id => $entity_type) { // The ContentModerationState entity type should never be moderated. - if ($entity_type->isRevisionable() && $entity_type_id != 'content_moderation_state') { + if ($entity_type instanceof ContentEntityTypeInterface && $entity_type_id != 'content_moderation_state') { $entity_types[$entity_type_id] = $this->addModerationToEntityType($entity_type); // Add additional moderation support to entity types whose bundles are // managed by a config entity type. diff --git a/core/modules/content_moderation/src/ModerationInformation.php b/core/modules/content_moderation/src/ModerationInformation.php index c20be52..7398acc 100644 --- a/core/modules/content_moderation/src/ModerationInformation.php +++ b/core/modules/content_moderation/src/ModerationInformation.php @@ -73,6 +73,10 @@ public function shouldModerateEntitiesOfBundle(EntityTypeInterface $entity_type, * {@inheritdoc} */ public function getLatestRevision($entity_type_id, $entity_id) { + // Non-revisionable entities only have a single revision. + if (!$this->entityTypeManager->getDefinition($entity_type_id)->isRevisionable()) { + return $this->entityTypeManager->getStorage($entity_type_id)->load($entity_id); + } if ($latest_revision_id = $this->getLatestRevisionId($entity_type_id, $entity_id)) { return $this->entityTypeManager->getStorage($entity_type_id)->loadRevision($latest_revision_id); } diff --git a/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php b/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php index 0444644..485477d 100644 --- a/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php +++ b/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php @@ -54,12 +54,19 @@ protected function loadContentModerationStateRevision(ContentEntityInterface $en $moderation_info = \Drupal::service('content_moderation.moderation_information'); $content_moderation_storage = \Drupal::entityTypeManager()->getStorage('content_moderation_state'); + if ($entity->getEntityType()->isRevisionable()) { + $revision_id = $entity->isNewRevision() ? $entity->getLoadedRevisionId() : $entity->getRevisionId(); + } + else { + $revision_id = $entity->id(); + } + $revisions = \Drupal::service('entity.query')->get('content_moderation_state') ->condition('content_entity_type_id', $entity->getEntityTypeId()) ->condition('content_entity_id', $entity->id()) // Ensure the correct revision is loaded in scenarios where a revision is // being reverted. - ->condition('content_entity_revision_id', $entity->isNewRevision() ? $entity->getLoadedRevisionId() : $entity->getRevisionId()) + ->condition('content_entity_revision_id', $revision_id) ->condition('workflow', $moderation_info->getWorkflowForEntity($entity)->id()) ->allRevisions() ->sort('revision_id', 'DESC') diff --git a/core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php b/core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php index a1c7542..249f370 100644 --- a/core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php +++ b/core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php @@ -64,14 +64,59 @@ protected function setUp() { /** * Tests basic monolingual content moderation through the API. * - * @dataProvider basicModerationTestCases + * @dataProvider nonRevisionableEntityTestCases */ - public function testBasicModeration($entity_type_id) { - // Make the 'entity_test_with_bundle' entity type revisionable. - if ($entity_type_id == 'entity_test_with_bundle') { - $this->setEntityTestWithBundleKeys(['revision' => 'revision_id']); + public function testNonRevisionableEntity($entity_type_id) { + $entity_storage = $this->entityTypeManager->getStorage($entity_type_id); + $bundle_id = $entity_type_id; + $bundle_entity_type_id = $this->entityTypeManager->getDefinition($entity_type_id)->getBundleEntityType(); + if ($bundle_entity_type_id) { + $bundle_entity_type_definition = $this->entityTypeManager->getDefinition($bundle_entity_type_id); + $entity_type_storage = $this->entityTypeManager->getStorage($bundle_entity_type_id); + + $entity_type = $entity_type_storage->create([ + $bundle_entity_type_definition->getKey('id') => 'example', + ]); + $entity_type->save(); + $bundle_id = $entity_type->id(); } + $workflow = Workflow::load('editorial'); + $workflow->getTypePlugin()->addEntityTypeAndBundle($entity_type_id, $bundle_id); + $workflow->save(); + + $entity = $entity_storage->create([ + 'title' => 'Test title', + $this->entityTypeManager->getDefinition($entity_type_id)->getKey('bundle') => $bundle_id, + ]); + $entity->save(); + $entity = $this->reloadEntity($entity); + $this->assertEquals('draft', $entity->moderation_state->value); + + $entity->moderation_state->value = 'published'; + $entity->save(); + + $entity = $this->reloadEntity($entity); + $this->assertEquals('published', $entity->moderation_state->value); + } + + /** + * Dataprovider for ::testNonRevisionableEntity + */ + public function nonRevisionableEntityTestCases() { + return [ + 'Test Entity with Bundle' => [ + 'entity_test_with_bundle', + ], + ]; + } + + /** + * Tests basic monolingual content moderation through the API. + * + * @dataProvider basicModerationTestCases + */ + public function testBasicModeration($entity_type_id) { $entity_storage = $this->entityTypeManager->getStorage($entity_type_id); $bundle_id = $entity_type_id; $bundle_entity_type_id = $this->entityTypeManager->getDefinition($entity_type_id)->getBundleEntityType(); @@ -170,15 +215,12 @@ public function basicModerationTestCases() { 'Block content' => [ 'block_content', ], - 'Test Entity with Bundle' => [ - 'entity_test_with_bundle', - ], 'Test entity - revisions, data table, and published interface' => [ 'entity_test_mulrevpub', ], 'Entity Test with revisions' => [ 'entity_test_rev', - ] + ], ]; }