class MediaMappingsConstraintValidator

Same name in other branches
  1. 11.x core/modules/media/src/Plugin/Validation/Constraint/MediaMappingsConstraintValidator.php \Drupal\media\Plugin\Validation\Constraint\MediaMappingsConstraintValidator

Validates media mappings.

Hierarchy

Expanded class hierarchy of MediaMappingsConstraintValidator

File

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

Namespace

Drupal\media\Plugin\Validation\Constraint
View source
class MediaMappingsConstraintValidator extends ConstraintValidator {
    
    /**
     * {@inheritdoc}
     */
    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.
            }
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary
MediaMappingsConstraintValidator::validate public function

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