Commit 0ec4d706 authored by RoSk0's avatar RoSk0

Added custom UniqueReference constraint to relation fields.

parent 881b675d
......@@ -117,6 +117,9 @@ class Relation extends ContentEntityBase implements RelationInterface {
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
->setRequired(TRUE)
->setCardinality(1)
// @todo Replace with what ever would work with entity reference
// from core. https://www.drupal.org/project/drupal/issues/2973455
->addConstraint('UniqueReference')
->setLabel(t('User'))
->setDescription(t('The user ID of the relation.'))
->setSetting('target_type', 'user')
......@@ -140,6 +143,9 @@ class Relation extends ContentEntityBase implements RelationInterface {
$fields['individual_id'] = BaseFieldDefinition::create('entity_reference')
->setRequired(TRUE)
->setCardinality(1)
// @todo Replace with what ever would work with entity reference
// from core. https://www.drupal.org/project/drupal/issues/2973455
->addConstraint('UniqueReference')
->setLabel(t('Individual'))
->setDescription(t('The individual ID of the relation.'))
->setSetting('target_type', 'crm_core_individual')
......
<?php
namespace Drupal\crm_core_user_sync\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
/**
* Entity Reference unique reference constraint.
*
* Verifies that referenced entities referenced only once.
*
* @Constraint(
* id = "UniqueReference",
* label = @Translation("Unique Entity Reference reference", context = "Validation")
* )
*/
class UniqueReferenceConstraint extends Constraint {
/**
* The default violation message.
*
* @var string
*/
public $message = 'A @entity_type with @field_name referencing entity with ID @id already exists.';
}
<?php
namespace Drupal\crm_core_user_sync\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
/**
* Validates that a field is unique for the given entity type.
*/
class UniqueReferenceConstraintValidator extends ConstraintValidator {
/**
* {@inheritdoc}
*/
public function validate($items, Constraint $constraint) {
if (!$item = $items->first()) {
return;
}
$field_name = $items->getFieldDefinition()->getName();
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$entity = $items->getEntity();
$entity_type_id = $entity->getEntityTypeId();
$id_key = $entity->getEntityType()->getKey('id');
$id = (int) $items->getEntity()->id();
$target_id = $item->target_id;
$value_taken = (bool) \Drupal::entityQuery($entity_type_id)
// The id could be NULL, so we cast it to 0 in that case.
->condition($id_key, $id, '<>')
->condition($field_name, $target_id)
->range(0, 1)
->count()
->execute();
if ($value_taken) {
$this->context->addViolation($constraint->message, [
'@id' => $target_id,
'@entity_type' => $entity->getEntityType()->getLowercaseLabel(),
'@field_name' => mb_strtolower($items->getFieldDefinition()->getLabel()),
]);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment