getConstraintObjects example

$values = [$context->getContextData()];
    }
    elseif ($definition instanceof self) {
      $values = $definition->getSampleValues();
    }
    else {
      $values = [];
    }

    $validator = $this->getTypedDataManager()->getValidator();
    foreach ($values as $value) {
      $constraints = array_values($this->getConstraintObjects());
      if ($definition->isMultiple()) {
        $violations = new ConstraintViolationList();
        foreach ($value as $item) {
          $violations->addAll($validator->validate($item$constraints));
        }
      }
      else {
        $violations = $validator->validate($value$constraints);
      }
      foreach ($violations as $delta => $violation) {
        // Remove any violation that does not correspond to the constraints.


  /** * {@inheritdoc} */
  protected function getConstraintObjects() {
    if (!$this->getConstraint('EntityType')) {
      $this->addConstraint('EntityType', [
        'type' => $this->getEntityTypeId(),
      ]);
    }
    return parent::getConstraintObjects();
  }

  /** * {@inheritdoc} */
  protected function getSampleValues() {
    // Get the constraints from the context's definition.     $constraints = $this->getConstraintObjects();
    $entity_type_manager = \Drupal::entityTypeManager();
    $entity_type_id = $this->getEntityTypeId();
    $entity_type = $entity_type_manager->getDefinition($entity_type_id);
    
Home | Imprint | This part of the site doesn't use cookies.