You are here

public function File::validate in Entity Browser 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/EntityBrowser/WidgetValidation/File.php \Drupal\entity_browser\Plugin\EntityBrowser\WidgetValidation\File::validate()

Validates the widget.

Parameters

array $entities: Array of selected entities.

array $options: (Optional) Array of options needed by the constraint validator.

Return value

\Symfony\Component\Validator\ConstraintViolationListInterface A list of constraint violations. If the list is empty, validation succeeded.

Overrides WidgetValidationBase::validate

File

src/Plugin/EntityBrowser/WidgetValidation/File.php, line 22

Class

File
Validates a file based on passed validators.

Namespace

Drupal\entity_browser\Plugin\EntityBrowser\WidgetValidation

Code

public function validate(array $entities, array $options = []) {
  $violations = new ConstraintViolationList();

  // We implement the same logic as \Drupal\file\Plugin\Validation\Constraint\FileValidationConstraintValidator
  // here as core does not always write constraints with non-form use cases
  // in mind.
  foreach ($entities as $entity) {
    if (isset($options['validators'])) {

      // Checks that a file meets the criteria specified by the validators.
      if ($errors = file_validate($entity, $options['validators'])) {
        foreach ($errors as $error) {
          $violation = new ConstraintViolation($error, $error, [], $entity, '', $entity);
          $violations
            ->add($violation);
        }
      }
    }
  }
  return $violations;
}