You are here

public function ModerationStateValidator::validate in Workbench Moderation 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/Validation/Constraint/ModerationStateValidator.php \Drupal\workbench_moderation\Plugin\Validation\Constraint\ModerationStateValidator::validate()

File

src/Plugin/Validation/Constraint/ModerationStateValidator.php, line 62

Class

ModerationStateValidator

Namespace

Drupal\workbench_moderation\Plugin\Validation\Constraint

Code

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
    ->isModeratableEntity($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());
  }
  $next_moderation_state_id = $entity->moderation_state->target_id;
  $original_moderation_state_id = $original_entity->moderation_state->target_id;
  if (!$this->validation
    ->isTransitionAllowed($original_moderation_state_id, $next_moderation_state_id)) {
    $this->context
      ->addViolation($constraint->message, [
      '%from' => $original_entity->moderation_state->entity
        ->label(),
      '%to' => $entity->moderation_state->entity
        ->label(),
    ]);
  }
}