3 namespace Drupal\media_entity_twitter\Plugin\Validation\Constraint;
5 use Drupal\media_entity_twitter\Plugin\media\Source\Twitter;
6 use Drupal\Core\Field\FieldItemList;
7 use Drupal\Core\Field\FieldItemInterface;
8 use Symfony\Component\Validator\Constraint;
9 use Symfony\Component\Validator\ConstraintValidator;
12 * Validates the TweetEmbedCode constraint.
14 class TweetEmbedCodeConstraintValidator extends ConstraintValidator {
19 public function validate($value, Constraint $constraint) {
21 if (is_string($value)) {
24 elseif ($value instanceof FieldItemList) {
25 $fieldtype = $value->getFieldDefinition()->getType();
26 $field_value = $value->getValue();
27 if ($fieldtype == 'link') {
28 $data = empty($field_value[0]['uri']) ? "" : $field_value[0]['uri'];
31 $data = empty($field_value[0]['value']) ? "" : $field_value[0]['value'];
34 elseif ($value instanceof FieldItemInterface) {
35 $class = get_class($value);
36 $property = $class::mainPropertyName();
38 $data = $value->{$property};
43 foreach (Twitter::$validationRegexp as $pattern => $key) {
44 if (preg_match($pattern, $data, $item_matches)) {
45 $matches[] = $item_matches;
48 if (empty($matches)) {
49 $this->context->addViolation($constraint->message);