UserIdConstraintValidator.php in Scheduled Updates 8
File
tests/modules/scheduled_updates_tests/src/Plugin/Validation/Constraint/UserIdConstraintValidator.php
View source
<?php
namespace Drupal\scheduled_updates_tests\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class UserIdConstraintValidator extends ConstraintValidator {
const CONSTRAINT_NONE = 'CONSTRAINT_NONE';
const CONSTRAINT_ANONYMOUS = 'CONSTRAINT_ANONYMOUS';
const CONSTRAINT_USER_1 = 'CONSTRAINT_USER_1';
public function validate($items, Constraint $constraint) {
$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");
}
}
}
}