summaryrefslogtreecommitdiffstats
path: root/core/modules/media/tests/modules/media_test_source/src/Plugin/Validation/Constraint/MediaTestConstraintValidator.php
blob: 1c744430a5fb905d9c707fb3db18d9963ff93101 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php

namespace Drupal\media_test_source\Plugin\Validation\Constraint;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

/**
 * Validates the MediaTestConstraint.
 */
class MediaTestConstraintValidator extends ConstraintValidator {

  /**
   * {@inheritdoc}
   */
  public function validate($value, Constraint $constraint) {
    if ($value instanceof EntityInterface) {
      $string_to_test = $value->label();
    }
    elseif ($value instanceof FieldItemListInterface) {
      $string_to_test = $value->value;
    }
    else {
      return;
    }

    if (strpos($string_to_test, 'love Drupal') === FALSE) {
      $this->context->addViolation($constraint->message);
    }
  }

}