You are here

public function UserIdConstraintValidator::validate in Scheduled Updates 8

File

tests/modules/scheduled_updates_tests/src/Plugin/Validation/Constraint/UserIdConstraintValidator.php, line 17

Class

UserIdConstraintValidator

Namespace

Drupal\scheduled_updates_tests\Plugin\Validation\Constraint

Code

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

  /** @var \Drupal\Core\Field\FieldItemListInterface $items */

  /** @var \Drupal\Core\Entity\EntityInterface $entity */
  $entity = $items
    ->getParent()
    ->getValue();
  if ($entity
    ->getEntityTypeId() !== 'node') {
    return;
  }
  $constraint = \Drupal::state()
    ->get('constraint_uid', static::CONSTRAINT_NONE);
  if ($constraint !== static::CONSTRAINT_NONE) {
    if ($constraint === static::CONSTRAINT_USER_1 && (int) \Drupal::currentUser()
      ->id() !== 1) {
      throw new \LogicException("Only uid 1 validates");
    }
    elseif ($constraint === static::CONSTRAINT_ANONYMOUS && !\Drupal::currentUser()
      ->isAnonymous()) {
      throw new \LogicException("Only uid 1 validates");
    }
  }
}