- $original_entity = $this->moderationInformation->getLatestRevision($entity->getEntityTypeId(), $entity->id());
- if (!$entity->isDefaultTranslation() && $original_entity->hasTranslation($entity->language()->getId())) {
- $original_entity = $original_entity->getTranslation($entity->language()->getId());
- }
-
- $workflow = $this->moderationInformation->getWorkflowForEntity($entity);
- $new_state = $workflow->getState($entity->moderation_state->value) ?: $workflow->getInitialState();
- $original_state = $workflow->getState($original_entity->moderation_state->value);
- // @todo - what if $new_state references something that does not exist or
- // is null.
- if (!$original_state->canTransitionTo($new_state->id())) {
- $this->context->addViolation($constraint->message, ['%from' => $original_state->label(), '%to' => $new_state->label()]);
+ // If a new state is being set and there is an existing state, validate
+ // there is a valid transition between them.
+ if (!$entity->isNew() && !$this->isFirstTimeModeration($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 the state of the original entity doesn't exist on the workflow,
+ // we cannot do any further validation of transitions, because none will
+ // be setup for a state that doesn't exist. Instead allow any state to
+ // take its place.
+ if (!$workflow->getTypePlugin()->hasState($original_entity->moderation_state->value)) {
+ return;
+ }
+
+ $new_state = $workflow->getTypePlugin()->getState($entity->moderation_state->value);
+ $original_state = $workflow->getTypePlugin()->getState($original_entity->moderation_state->value);
+
+ if (!$original_state->canTransitionTo($new_state->id())) {
+ $this->context->addViolation($constraint->message, [
+ '%from' => $original_state->label(),
+ '%to' => $new_state->label()
+ ]);
+ }