namespace Drupal\media_entity_instagram\Plugin\Validation\Constraint;
-use Drupal\media_entity\EmbedCodeValueTrait;
-use Drupal\media_entity_instagram\Plugin\MediaEntity\Type\Instagram;
+use Drupal\Core\Field\FieldItemInterface;
+use Drupal\media_entity_instagram\Plugin\media\Source\Instagram;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
*/
class InstagramEmbedCodeConstraintValidator extends ConstraintValidator {
- use EmbedCodeValueTrait;
-
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint) {
- $value = $this->getEmbedCode($value);
- if (!isset($value)) {
- return;
+ $data = '';
+ if (is_string($value)) {
+ $data = $value;
}
-
- $matches = [];
- foreach (Instagram::$validationRegexp as $pattern => $key) {
- if (preg_match($pattern, $value, $item_matches)) {
- $matches[] = $item_matches;
+ elseif ($value instanceof FieldItemInterface) {
+ $class = get_class($value);
+ $property = $class::mainPropertyName();
+ if ($property) {
+ $data = $value->{$property};
}
}
- if (empty($matches)) {
- $this->context->addViolation($constraint->message);
+ if ($data) {
+ $matches = [];
+ foreach (Instagram::$validationRegexp as $pattern => $key) {
+ if (preg_match($pattern, $data, $item_matches)) {
+ $matches[] = $item_matches;
+ }
+ }
+ if (empty($matches)) {
+ $this->context->addViolation($constraint->message);
+ }
}
+
}
}