You are here

class VideoEmbedConstraintValidator in Video Embed Field 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/Validation/Constraint/VideoEmbedConstraintValidator.php \Drupal\video_embed_field\Plugin\Validation\Constraint\VideoEmbedConstraintValidator

Validates the video embed providers.

Hierarchy

Expanded class hierarchy of VideoEmbedConstraintValidator

File

src/Plugin/Validation/Constraint/VideoEmbedConstraintValidator.php, line 14

Namespace

Drupal\video_embed_field\Plugin\Validation\Constraint
View source
class VideoEmbedConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {

  /**
   * Video embed provider manager service.
   *
   * @var \Drupal\video_embed_field\ProviderManagerInterface
   */
  protected $providerManager;

  /**
   * Create an instance of the validator.
   *
   * @param \Drupal\video_embed_field\ProviderManagerInterface $provider_manager
   *   The provider manager service.
   */
  public function __construct(ProviderManagerInterface $provider_manager) {
    $this->providerManager = $provider_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('video_embed_field.provider_manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function validate($field, Constraint $constraint) {
    if (!isset($field->value)) {
      return NULL;
    }
    $allowed_providers = $field
      ->getFieldDefinition()
      ->getSetting('allowed_providers');
    $allowed_provider_definitions = $this->providerManager
      ->loadDefinitionsFromOptionList($allowed_providers);
    if (FALSE === $this->providerManager
      ->filterApplicableDefinitions($allowed_provider_definitions, $field->value)) {
      $this->context
        ->addViolation($constraint->message);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
VideoEmbedConstraintValidator::$providerManager protected property Video embed provider manager service.
VideoEmbedConstraintValidator::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
VideoEmbedConstraintValidator::validate public function Checks if the passed value is valid.
VideoEmbedConstraintValidator::__construct public function Create an instance of the validator.