/** * {@inheritdoc} */ public function validate($value, Constraint $constraint) { /** @var \Drupal\Core\Entity\EntityInterface $entity */ $entity = $value->getEntity(); // Ignore entities that are not subject to moderation anyway. if (!$this->moderationInformation->isModeratedEntity($entity)) { return; } // Ignore entities that are being created for the first time. if ($entity->isNew()) { return; } // Ignore entities that are being moderated for the first time, such as // when they existed before moderation was enabled for this entity type. if ($this->isFirstTimeModeration($entity)) { return; } $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()); } if ($entity->moderation_state->target_id) { $new_state_id = $entity->moderation_state->target_id; } else { $new_state_id = $default = $this->moderationInformation->loadBundleEntity($entity->getEntityType()->getBundleEntityType(), $entity->bundle())->getThirdPartySetting('content_moderation', 'default_moderation_state'); } if ($new_state_id) { $new_state = ModerationStateEntity::load($new_state_id); } // @todo - what if $new_state_id references something that does not exist or // is null. if (!$this->validation->isTransitionAllowed($original_entity->moderation_state->entity, $new_state)) { $this->context->addViolation($constraint->message, ['%from' => $original_entity->moderation_state->entity->label(), '%to' => $new_state->label()]); } }
/** * @covers ::isTransitionAllowed * @covers ::calculatePossibleTransitions * * @dataProvider providerIsTransitionAllowedWithInValidTransition */ public function testIsTransitionAllowedWithInValidTransition($from_id, $to_id) { $storage = $this->setupStateStorage(); $state_transition_validation = new StateTransitionValidation($this->setupEntityTypeManager($storage), $this->setupQueryFactory()); $this->assertFalse($state_transition_validation->isTransitionAllowed($storage->load($from_id), $storage->load($to_id))); }