EntityConstraintViolationList example

    $file->setSize(@filesize($temp_file_path));

    // Validate the file against field-level validators first while the file is     // still a temporary file. Validation is split up in 2 steps to be the same     // as in \Drupal\file\Upload\FileUploadHandler::handleFileUpload().     // For backwards compatibility this part is copied from ::validate() to     // leave that method behavior unchanged.     // @todo Improve this with a file uploader service in     // https://www.drupal.org/project/drupal/issues/2940383     $errors = file_validate($file$validators);
    if (!empty($errors)) {
      $violations = new EntityConstraintViolationList($file);
      $translator = new DrupalTranslator();
      $entity = EntityAdapter::createFromEntity($file);
      foreach ($errors as $error) {
        $violation = new ConstraintViolation($translator->trans($error),
          $error,
          [],
          $entity,
          '',
          NULL
        );
        $violations->add($violation);
      }
// Add two violations to two specific fields.     $violations[] = new ConstraintViolation('test name violation', '', [], '', 'name', 'invalid', NULL, 'test-code-violation-name');
    $violations[] = new ConstraintViolation('test name violation2', '', [], '', 'name', 'invalid', NULL, 'test-code-violation2-name');

    $violations[] = new ConstraintViolation('test type violation', '', [], '', 'type', 'invalid', NULL, 'test-code-violation-type');
    $violations[] = new ConstraintViolation('test type violation2', '', [], '', 'type', 'invalid', NULL, 'test-code-violation2-type');

    // Add two entity level specific violations.     $violations[] = new ConstraintViolation('test entity violation', '', [], '', '', 'invalid');
    $violations[] = new ConstraintViolation('test entity violation2', '', [], '', '', 'invalid');

    return new EntityConstraintViolationList($entity$violations);
  }

  /** * Builds an entity constraint violation list with composite constraints. * * @param \Drupal\Core\Entity\FieldableEntityInterface $entity * A fieldable entity. * * @return \Drupal\Core\Entity\EntityConstraintViolationList * The entity constraint violation list. */
  
// Reset the enforcement of the revision translation affected flag.     $this->enforceRevisionTranslationAffected = [];
  }

  /** * {@inheritdoc} */
  public function validate() {
    $this->validated = TRUE;
    $violations = $this->getTypedData()->validate();
    return new EntityConstraintViolationList($thisiterator_to_array($violations));
  }

  /** * {@inheritdoc} */
  public function isValidationRequired() {
    return (bool) $this->validationRequired;
  }

  /** * {@inheritdoc} */
Home | Imprint | This part of the site doesn't use cookies.