You are here

public function SchedulerModerationConstraintValidator::validate in Scheduler content moderation integration 8

File

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

Class

SchedulerModerationConstraintValidator
Validates the SchedulerModeration constraint.

Namespace

Drupal\scheduler_content_moderation_integration\Plugin\Validation\Constraint

Code

public function validate($value, Constraint $constraint) {

  // If the input parameters is something unexpected lets skip the validation.
  if (!$value instanceof FieldItemListInterface || !$constraint instanceof SchedulerModerationConstraint) {
    return;
  }

  // No need to validate if a moderation state has not ben set.
  if ($value
    ->isEmpty()) {
    return;
  }
  $entity = $value
    ->getEntity();

  // We can only work with content entities so lets skip validation if it's
  // something else.
  if (!$entity instanceof ContentEntityInterface) {
    return;
  }

  /** @var \Drupal\options\Plugin\Field\FieldType\ListStringItem $field */
  $field = $value
    ->first();
  $moderation_state = $field
    ->get('value')
    ->getValue();
  if (!$this
    ->isValidTransition($moderation_state, $entity)) {
    $this->context
      ->buildViolation($constraint->messagePublishModerationInvalid)
      ->atPath($value
      ->getName())
      ->addViolation();
  }
}