f4ebd0fe6638e08b1b0db8e9b0179e1e4ef9f96c
[yaffs-website] / web / modules / contrib / video_embed_field / src / Plugin / Validation / Constraint / VideoEmbedConstraintValidator.php
1 <?php
2
3 namespace Drupal\video_embed_field\Plugin\Validation\Constraint;
4
5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
6 use Drupal\video_embed_field\ProviderManagerInterface;
7 use Symfony\Component\DependencyInjection\ContainerInterface;
8 use Symfony\Component\Validator\Constraint;
9 use Symfony\Component\Validator\ConstraintValidator;
10
11 /**
12  * Validates the video embed providers.
13  */
14 class VideoEmbedConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
15
16   /**
17    * Video embed provider manager service.
18    *
19    * @var \Drupal\video_embed_field\ProviderManagerInterface
20    */
21   protected $providerManager;
22
23   /**
24    * Create an instance of the validator.
25    *
26    * @param \Drupal\video_embed_field\ProviderManagerInterface $provider_manager
27    *   The provider manager service.
28    */
29   public function __construct(ProviderManagerInterface $provider_manager) {
30     $this->providerManager = $provider_manager;
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   public static function create(ContainerInterface $container) {
37     return new static(
38       $container->get('video_embed_field.provider_manager')
39     );
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public function validate($field, Constraint $constraint) {
46     if (!isset($field->value)) {
47       return NULL;
48     }
49
50     $allowed_providers = $field->getFieldDefinition()->getSetting('allowed_providers');
51     $allowed_provider_definitions = $this->providerManager->loadDefinitionsFromOptionList($allowed_providers);
52
53     if (FALSE === $this->providerManager->filterApplicableDefinitions($allowed_provider_definitions, $field->value)) {
54       $this->context->addViolation($constraint->message);
55     }
56   }
57
58 }