diff --git a/modules/video_embed_wysiwyg/src/Form/VideoEmbedDialog.php b/modules/video_embed_wysiwyg/src/Form/VideoEmbedDialog.php index e9cefe0..b2d80fd 100644 --- a/modules/video_embed_wysiwyg/src/Form/VideoEmbedDialog.php +++ b/modules/video_embed_wysiwyg/src/Form/VideoEmbedDialog.php @@ -143,7 +143,7 @@ class VideoEmbedDialog extends FormBase { $provider = $this->getProvider($form_state->getValue('video_url')); // Display an error if no provider can be loaded for this video. if (FALSE == $provider) { - $form_state->setError($form['video_url'], VideoTextfield::getProviderErrorMessage()); + $form_state->setError($form['video_url'], $this->t('Could not find a video provider to handle the given URL.')); return; } } diff --git a/src/Plugin/Field/FieldType/VideoEmbedField.php b/src/Plugin/Field/FieldType/VideoEmbedField.php index 16867fd..3a66618 100644 --- a/src/Plugin/Field/FieldType/VideoEmbedField.php +++ b/src/Plugin/Field/FieldType/VideoEmbedField.php @@ -17,7 +17,8 @@ use Drupal\Core\TypedData\TraversableTypedDataInterface; * description = @Translation("Stores a video and then outputs some embed code."), * category = @Translation("Media"), * default_widget = "video_embed_field_textfield", - * default_formatter = "video_embed_field_video" + * default_formatter = "video_embed_field_video", + * constraints = {"VideoEmbedValidation" = {}} * ) */ class VideoEmbedField extends FieldItemBase { diff --git a/src/Plugin/Field/FieldWidget/VideoTextfield.php b/src/Plugin/Field/FieldWidget/VideoTextfield.php index 4085902..5a5d84c 100644 --- a/src/Plugin/Field/FieldWidget/VideoTextfield.php +++ b/src/Plugin/Field/FieldWidget/VideoTextfield.php @@ -30,41 +30,8 @@ class VideoTextfield extends WidgetBase { '#maxlength' => $this->getFieldSetting('max_length'), '#attributes' => ['class' => ['js-text-full', 'text-full']], '#allowed_providers' => $this->getFieldSetting('allowed_providers'), - '#element_validate' => [ - [get_class($this), 'validateFormElement'], - ], ]; return $element; } - /** - * Form element validation handler for URL alias form element. - * - * @param array $element - * The form element. - * @param \Drupal\Core\Form\FormStateInterface $form_state - * The form state. - */ - public static function validateFormElement(array &$element, FormStateInterface $form_state) { - $value = $element['#value']; - if (empty($value)) { - return; - } - $provider_manager = \Drupal::service('video_embed_field.provider_manager'); - $enabled_providers = $provider_manager->loadDefinitionsFromOptionList($element['#allowed_providers']); - if (!$provider_manager->filterApplicableDefinitions($enabled_providers, $value)) { - $form_state->setError($element, static::getProviderErrorMessage()); - } - } - - /** - * Get the error message indicating a provider could not be found. - * - * @return string - * The provider error message. - */ - public static function getProviderErrorMessage() { - return t('Could not find a video provider to handle the given URL.'); - } - } diff --git a/src/Plugin/Validation/Constraint/VideoEmbedConstraint.php b/src/Plugin/Validation/Constraint/VideoEmbedConstraint.php new file mode 100644 index 0000000..aeef70f --- /dev/null +++ b/src/Plugin/Validation/Constraint/VideoEmbedConstraint.php @@ -0,0 +1,24 @@ +get('video_embed_field.provider_manager') + ); + } + + /** + * 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 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); + } + } + +} diff --git a/tests/src/Kernel/ConstraintTest.php b/tests/src/Kernel/ConstraintTest.php new file mode 100644 index 0000000..1ed6323 --- /dev/null +++ b/tests/src/Kernel/ConstraintTest.php @@ -0,0 +1,53 @@ +installEntitySchema('user'); + $this->installSchema('system', ['sequences']); + + $this->user = $this->createUser([]); + } + + /** + * Test the video embed constraint. + */ + public function testConstraint() { + $entity = EntityTest::create(['user_id' => $this->user->id()]); + $entity->{$this->fieldName}->value = 'invalid URL'; + $violations = $entity->validate(); + + $this->assertCount(1, $violations); + $this->assertInstanceOf(VideoEmbedConstraint::class, $violations[0]->getConstraint()); + + $entity->{$this->fieldName}->value = 'https://youtube.com/watch?v=fdbFV_Wup-Ssw'; + $violations = $entity->validate(); + $this->assertCount(0, $violations); + } + +}