You are here

public function ModerationStateValidator::validate in Workbench Moderation 8

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

File

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

Class

ModerationStateValidator
Class for validating moderations states.

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;
  }
  $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());
  }

  /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $bundle */
  $bundle = $this->entityTypeManager
    ->getStorage($entity
    ->getEntityType()
    ->getBundleEntityType())
    ->load($entity
    ->bundle());
  $default_state = $bundle
    ->getThirdPartySetting('workbench_moderation', 'default_moderation_state');
  $next_moderation_state = ModerationState::load(!$entity->moderation_state
    ->isEmpty() ? $entity->moderation_state->target_id : $default_state);
  $original_moderation_state = ModerationState::load($original_entity && !$original_entity->moderation_state
    ->isEmpty() ? $original_entity->moderation_state->target_id : $default_state);
  if (!$this->validation
    ->isTransitionAllowed($original_moderation_state
    ->id(), $next_moderation_state
    ->id())) {
    $this->context
      ->addViolation($constraint->message, [
      '%from' => $original_moderation_state
        ->label(),
      '%to' => $next_moderation_state
        ->label(),
    ]);
  }
  elseif (!$this->validation
    ->userMayTransition($original_moderation_state
    ->id(), $next_moderation_state
    ->id(), $this->currentUser)) {
    $this->context
      ->addViolation($constraint->accessDeniedMessage, [
      '%from' => $original_moderation_state
        ->label(),
      '%to' => $next_moderation_state
        ->label(),
    ]);
  }
}