public function UniqueBirthLogConstraintValidator::validate in farmOS 2.x
File
- modules/
log/ birth/ src/ Plugin/ Validation/ Constraint/ UniqueBirthLogConstraintValidator.php, line 45
Class
- UniqueBirthLogConstraintValidator
- Validates the UniqueBirthLog constraint.
Namespace
Drupal\farm_birth\Plugin\Validation\ConstraintCode
public function validate($value, Constraint $constraint) {
/** @var \Drupal\Core\Field\EntityReferenceFieldItemList $value */
/** @var \Drupal\farm_birth\Plugin\Validation\Constraint\UniqueBirthLogConstraint $constraint */
// Only continue if this is a birth log.
/** @var \Drupal\log\Entity\LogInterface $log */
$log = $value
->getParent()
->getValue();
if (!empty($log) && $log
->bundle() != 'birth') {
return;
}
// Iterate through referenced entities.
foreach ($value
->referencedEntities() as $delta => $asset) {
// If the log is not new, skip validation.
// A birth log exits so there is no need to check if one can be created.
/** @var \Drupal\log\Entity\LogInterface $log */
$log = $value
->getParent()
->getValue();
if (!$log
->isNew()) {
return;
}
// Query the number of birth logs that reference the asset.
// We do not check access to ensure that all matching logs are found.
$count = $this->entityTypeManager
->getStorage('log')
->getAggregateQuery()
->accessCheck(FALSE)
->condition('type', 'birth')
->condition('asset', $asset
->id())
->count()
->execute();
// If more than 0 birth logs reference the asset, add a violation.
if ($count > 0) {
$this->context
->buildViolation($constraint->message, [
'%child' => $asset
->label(),
])
->atPath((string) $delta . '.target_id')
->setInvalidValue($asset
->id())
->addViolation();
}
}
}