salesforce-8.x-4.x-dev/modules/salesforce_mapping/src/Plugin/Validation/Constraint/MappingEntityTypeConstraintValidator.php
modules/salesforce_mapping/src/Plugin/Validation/Constraint/MappingEntityTypeConstraintValidator.php
<?php
namespace Drupal\salesforce_mapping\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
/**
* Validates that a salesforce mapping matches entity type of the given entity.
*/
class MappingEntityTypeConstraintValidator extends ConstraintValidator {
/**
* {@inheritdoc}
*/
public function validate(mixed $entity, Constraint $constraint) {
$drupal_entity = $entity->getMappedEntity() ?: $entity->getDrupalEntityStub();
if (!$drupal_entity) {
$this->context->addViolation('Validation failed. Please check your input and try again.');
return;
}
if ($drupal_entity->getEntityTypeId() != $entity->getMapping()->getDrupalEntityType()) {
$this->context->addViolation($constraint->message, [
'%mapping' => $entity->getMapping()->label(),
'%entity_type' => $drupal_entity->getEntityType()->getLabel(),
]);
}
}
}
