function ImmutablePropertiesConstraintValidator::validate
Same name in other branches
- 11.x core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/ImmutablePropertiesConstraintValidator.php \Drupal\Core\Entity\Plugin\Validation\Constraint\ImmutablePropertiesConstraintValidator::validate()
File
-
core/
lib/ Drupal/ Core/ Entity/ Plugin/ Validation/ Constraint/ ImmutablePropertiesConstraintValidator.php, line 43
Class
- ImmutablePropertiesConstraintValidator
- Validates the ImmutableProperties constraint.
Namespace
Drupal\Core\Entity\Plugin\Validation\ConstraintCode
public function validate(mixed $value, Constraint $constraint) {
assert($constraint instanceof ImmutablePropertiesConstraint);
if (!$value instanceof ConfigEntityInterface) {
throw new UnexpectedValueException($value, ConfigEntityInterface::class);
}
// This validation is irrelevant on new entities.
if ($value->isNew()) {
return;
}
$id = $value->getOriginalId() ?: $value->id();
if (empty($id)) {
throw new LogicException('The entity does not have an ID.');
}
$original = $this->entityTypeManager
->getStorage($value->getEntityTypeId())
->loadUnchanged($id);
if (empty($original)) {
throw new RuntimeException('The original entity could not be loaded.');
}
foreach ($constraint->properties as $name) {
// The property must be concretely defined in the class.
if (!property_exists($value, $name)) {
throw new LogicException("The entity does not have a '{$name}' property.");
}
if ($original->get($name) !== $value->get($name)) {
$this->context
->addViolation($constraint->message, [
'@name' => $name,
]);
}
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.