{% import '@lib/di.twig' as di %} context->addViolation($constraint->message); } } {% elseif input_type == 'item' %} public function validate(mixed $item, Constraint $constraint): void { if (!$item instanceof FieldItemInterface) { throw new \InvalidArgumentException( sprintf('The validated value must be instance of \Drupal\Core\Field\FieldItemInterface, %s was given.', get_debug_type($item)) ); } // @todo Validate the item value here. if ($item->value === 'wrong') { $this->context->addViolation($constraint->message); } } {% elseif input_type == 'item_list' %} public function validate(mixed $items, Constraint $constraint): void { if (!$items instanceof FieldItemListInterface) { throw new \InvalidArgumentException( sprintf('The validated value must be instance of \Drupal\Core\Field\FieldItemListInterface, %s was given.', get_debug_type($items)) ); } foreach ($items as $delta => $item) { // @todo Validate the item list here. if ($item->value === 'wrong') { $this->context->buildViolation($constraint->message) ->atPath($delta) ->addViolation(); } } } {% elseif input_type == 'entity' %} public function validate(mixed $entity, Constraint $constraint): void { if (!$entity instanceof EntityInterface) { throw new \InvalidArgumentException( sprintf('The validated value must be instance of \Drupal\Core\Entity\EntityInterface, %s was given.', get_debug_type($entity)) ); } // @todo Validate the entity here. if ($entity->label() === 'wrong') { // @DCG Use the following code to bind the violation to a specific field. // @code // $this->context->buildViolation($constraint->message) // ->atPath('field_example') // ->addViolation(); // @endcode $this->context->addViolation($constraint->message); } } {% endif %} }