Skip to content
Snippets Groups Projects
Commit 290afbd0 authored by Aaron Bauman's avatar Aaron Bauman
Browse files

Defensive coding

parent 48bcdc17
No related branches found
No related tags found
No related merge requests found
...@@ -34,20 +34,27 @@ class UniqueFieldsConstraintValidator extends ConstraintValidator { ...@@ -34,20 +34,27 @@ class UniqueFieldsConstraintValidator extends ConstraintValidator {
$value = $entity->{$field_name}->{$property}; $value = $entity->{$field_name}->{$property};
$query->condition($field, $value); $query->condition($field, $value);
} }
$id = $query->execute();
if ($id = $query->execute()) { if (empty($id)) {
$id = reset($id); return;
$entity = \Drupal::entityTypeManager() }
->getStorage($entity_type->id()) $id = reset($id);
->load($id); if (empty($id)) {
$url = $entity->toUrl(); return;
$message_replacements = [ }
'@entity_type' => $entity_type->getSingularLabel(), $entity = \Drupal::entityTypeManager()
':url' => $url->toString(), ->getStorage($entity_type->id())
'@label' => $entity->label(), ->load($id);
]; if (empty($entity)) {
$this->context->addViolation($constraint->message, $message_replacements); return;
} }
$url = $entity->toUrl();
$message_replacements = [
'@entity_type' => $entity_type->getSingularLabel(),
':url' => $url->toString(),
'@label' => $entity->label(),
];
$this->context->addViolation($constraint->message, $message_replacements);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment