244f1d881f685cda998943f20bd7484b84595ced
[yaffs-website] / Plugin / EntityBrowser / WidgetValidation / File.php
1 <?php
2
3 namespace Drupal\entity_browser\Plugin\EntityBrowser\WidgetValidation;
4
5 use Drupal\entity_browser\WidgetValidationBase;
6 use Symfony\Component\Validator\ConstraintViolation;
7 use Symfony\Component\Validator\ConstraintViolationList;
8
9 /**
10  * Validates a file based on passed validators.
11  *
12  * @EntityBrowserWidgetValidation(
13  *   id = "file",
14  *   label = @Translation("File validator")
15  * )
16  */
17 class File extends WidgetValidationBase {
18
19   /**
20    * {@inheritdoc}
21    */
22   public function validate(array $entities, $options = []) {
23     $violations = new ConstraintViolationList();
24
25     // We implement the same logic as \Drupal\file\Plugin\Validation\Constraint\FileValidationConstraintValidator
26     // here as core does not always write constraints with non-form use cases
27     // in mind.
28     foreach ($entities as $entity) {
29       if (isset($options['validators'])) {
30         // Checks that a file meets the criteria specified by the validators.
31         if ($errors = file_validate($entity, $options['validators'])) {
32           foreach ($errors as $error) {
33             $violation = new ConstraintViolation($error, $error, [], $entity, '', $entity);
34             $violations->add($violation);
35           }
36         }
37       }
38     }
39
40     return $violations;
41   }
42
43 }