function MediaMappingsConstraintValidator::validate

Same name in other branches
  1. 10 core/modules/media/src/Plugin/Validation/Constraint/MediaMappingsConstraintValidator.php \Drupal\media\Plugin\Validation\Constraint\MediaMappingsConstraintValidator::validate()

File

core/modules/media/src/Plugin/Validation/Constraint/MediaMappingsConstraintValidator.php, line 19

Class

MediaMappingsConstraintValidator
Validates media mappings.

Namespace

Drupal\media\Plugin\Validation\Constraint

Code

public function validate($value, Constraint $constraint) : void {
    if (!$constraint instanceof MediaMappingsConstraint) {
        throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\\MediaMappingsConstraint');
    }
    if (!$value instanceof MediaTypeInterface) {
        throw new UnexpectedTypeException($value, MediaTypeInterface::class);
    }
    // The source field cannot be the target of a field mapping because that
    // would cause it to be overwritten, possibly with invalid data. This is
    // also enforced in the UI.
    if (is_array($value->getFieldMap())) {
        try {
            $source_field_name = $value->getSource()
                ->getSourceFieldDefinition($value)?->getName();
            if (in_array($source_field_name, $value->getFieldMap(), TRUE)) {
                $this->context
                    ->addViolation($constraint->invalidMappingMessage, [
                    '@source_field_name' => $source_field_name,
                ]);
            }
        } catch (PluginException) {
            // The source references an invalid plugin.
        }
    }
}

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