diff --git a/core/modules/content_moderation/src/ModerationInformation.php b/core/modules/content_moderation/src/ModerationInformation.php index 922415efec..682f0665a1 100644 --- a/core/modules/content_moderation/src/ModerationInformation.php +++ b/core/modules/content_moderation/src/ModerationInformation.php @@ -243,17 +243,20 @@ public function getUnsupportedFeatures(EntityTypeInterface $entity_type) { /** * {@inheritdoc} */ - public function getOriginalOrInitialState(ContentEntityInterface $entity) { + public function getOriginalState(ContentEntityInterface $entity) { $state = NULL; $workflow_type = $this->getWorkflowForEntity($entity)->getTypePlugin(); if (!$entity->isNew() && !$this->isFirstTimeModeration($entity)) { - /** @var \Drupal\Core\Entity\ContentEntityInterface $original_entity */ - $original_entity = $this->entityTypeManager->getStorage($entity->getEntityTypeId())->loadRevision($entity->getLoadedRevisionId()); - if (!$entity->isDefaultTranslation() && $original_entity->hasTranslation($entity->language()->getId())) { - $original_entity = $original_entity->getTranslation($entity->language()->getId()); + if (empty($entity->original)) { + /** @var \Drupal\Core\Entity\ContentEntityInterface $original_entity */ + $original_entity = $this->entityTypeManager->getStorage($entity->getEntityTypeId())->loadRevision($entity->getLoadedRevisionId()); + if (!$entity->isDefaultTranslation() && $original_entity->hasTranslation($entity->language()->getId())) { + $original_entity = $original_entity->getTranslation($entity->language()->getId()); + } + $entity->original = $original_entity; } - if ($workflow_type->hasState($original_entity->moderation_state->value)) { - $state = $workflow_type->getState($original_entity->moderation_state->value); + if ($workflow_type->hasState($entity->original->moderation_state->value)) { + $state = $workflow_type->getState($entity->original->moderation_state->value); } } return $state ?: $workflow_type->getInitialState($entity); diff --git a/core/modules/content_moderation/src/ModerationInformationInterface.php b/core/modules/content_moderation/src/ModerationInformationInterface.php index 964fd371f6..99c35dd350 100644 --- a/core/modules/content_moderation/src/ModerationInformationInterface.php +++ b/core/modules/content_moderation/src/ModerationInformationInterface.php @@ -218,6 +218,6 @@ public function getUnsupportedFeatures(EntityTypeInterface $entity_type); * @return \Drupal\content_moderation\ContentModerationState * The original or default moderation state. */ - public function getOriginalOrInitialState(ContentEntityInterface $entity); + public function getOriginalState(ContentEntityInterface $entity); } diff --git a/core/modules/content_moderation/src/Plugin/Field/FieldWidget/ModerationStateWidget.php b/core/modules/content_moderation/src/Plugin/Field/FieldWidget/ModerationStateWidget.php index 78c10480aa..feaec24dfe 100644 --- a/core/modules/content_moderation/src/Plugin/Field/FieldWidget/ModerationStateWidget.php +++ b/core/modules/content_moderation/src/Plugin/Field/FieldWidget/ModerationStateWidget.php @@ -118,24 +118,28 @@ public function form(FieldItemListInterface $items, array &$form, FormStateInter */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ - $entity = $original_entity = $items->getEntity(); + $entity = $items->getEntity(); - $default = $this->moderationInformation->getOriginalOrInitialState($entity); + $default = $this->moderationInformation->getOriginalState($entity); // If the entity is not new, grab the most recent revision and // load it. The moderation state of the saved revision will be used // to display the current state as well determine the the appropriate // transitions. - if (!$entity->isNew()) { - /** @var \Drupal\Core\Entity\ContentEntityInterface $original_entity */ - $original_entity = $this->entityTypeManager->getStorage($entity->getEntityTypeId())->loadRevision($entity->getLoadedRevisionId()); - if (!$entity->isDefaultTranslation() && $original_entity->hasTranslation($entity->language()->getId())) { - $original_entity = $original_entity->getTranslation($entity->language()->getId()); + if (empty($entity->original)) { + $entity->original = $entity; + if (!$entity->isNew()) { + /** @var \Drupal\Core\Entity\ContentEntityInterface $original_entity */ + $original_entity = $this->entityTypeManager->getStorage($entity->getEntityTypeId())->loadRevision($entity->getLoadedRevisionId()); + if (!$entity->isDefaultTranslation() && $original_entity->hasTranslation($entity->language()->getId())) { + $original_entity = $original_entity->getTranslation($entity->language()->getId()); + } + $entity->original = $original_entity; } } /** @var \Drupal\workflows\Transition[] $transitions */ - $transitions = $this->validator->getValidTransitions($original_entity, $this->currentUser); + $transitions = $this->validator->getValidTransitions($entity->original, $this->currentUser); $transition_labels = []; $default_value = $items->value; diff --git a/core/modules/content_moderation/src/Plugin/Validation/Constraint/ModerationStateConstraintValidator.php b/core/modules/content_moderation/src/Plugin/Validation/Constraint/ModerationStateConstraintValidator.php index 7d6e88d054..e5b80eeb0a 100644 --- a/core/modules/content_moderation/src/Plugin/Validation/Constraint/ModerationStateConstraintValidator.php +++ b/core/modules/content_moderation/src/Plugin/Validation/Constraint/ModerationStateConstraintValidator.php @@ -108,7 +108,7 @@ public function validate($value, Constraint $constraint) { } $new_state = $workflow->getTypePlugin()->getState($entity->moderation_state->value); - $original_state = $this->moderationInformation->getOriginalOrInitialState($entity); + $original_state = $this->moderationInformation->getOriginalState($entity); // If a new state is being set and there is an existing state, validate // there is a valid transition between them.