DrupalTranslator example


  protected function validate(FileInterface $file, array $validators) {
    $violations = $file->validate();

    // Remove violations of inaccessible fields as they cannot stem from our     // changes.     $violations->filterByFieldAccess();

    // Validate the file based on the field definition configuration.     $errors = file_validate($file$validators);
    if (!empty($errors)) {
      $translator = new DrupalTranslator();
      foreach ($errors as $error) {
        $violation = new ConstraintViolation($translator->trans($error),
          (string) $error,
          [],
          EntityAdapter::createFromEntity($file),
          '',
          NULL
        );
        $violations->add($violation);
      }
    }

    
// 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);
      }
      

  public function setValidator(ValidatorInterface $validator) {
    $this->validator = $validator;
  }

  /** * {@inheritdoc} */
  public function getValidator() {
    if (!isset($this->validator)) {
      $this->validator = new RecursiveValidator(
        new ExecutionContextFactory(new DrupalTranslator()),
        new ConstraintValidatorFactory($this->classResolver),
        $this
      );
    }
    return $this->validator;
  }

  /** * {@inheritdoc} */
  public function setValidationConstraintManager(ConstraintManager $constraintManager) {
    
Home | Imprint | This part of the site doesn't use cookies.