MediaTestConstraintValidator.php

Same filename in other branches
  1. 9 core/modules/media/tests/modules/media_test_source/src/Plugin/Validation/Constraint/MediaTestConstraintValidator.php
  2. 8.9.x core/modules/media/tests/modules/media_test_source/src/Plugin/Validation/Constraint/MediaTestConstraintValidator.php
  3. 10 core/modules/media/tests/modules/media_test_source/src/Plugin/Validation/Constraint/MediaTestConstraintValidator.php

Namespace

Drupal\media_test_source\Plugin\Validation\Constraint

File

core/modules/media/tests/modules/media_test_source/src/Plugin/Validation/Constraint/MediaTestConstraintValidator.php

View source
<?php

declare (strict_types=1);
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) : void {
        if ($value instanceof EntityInterface) {
            $string_to_test = $value->label();
        }
        elseif ($value instanceof FieldItemListInterface) {
            $string_to_test = $value->value;
        }
        else {
            return;
        }
        if (!str_contains($string_to_test, 'love Drupal')) {
            $this->context
                ->addViolation($constraint->message);
        }
    }

}

Classes

Title Deprecated Summary
MediaTestConstraintValidator Validates the MediaTestConstraint.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.