1

I'm building a module that get information from the NCBI database API -- specifically it get's publications, from my group, by using a publication ID. I want anyone in the group to be able to add a publication to our site, however, to do this I need to make sure that the ID's being input are only input one time since they are all unique.

I've already set up a system that checks whether someone else has already submitted that value. I did this by building a constraint and constraint validator on that specific field that uses this query

$query = \Drupal::entityQuery($entity_type_id)
            ->condition($field_name, $id, '=')
            ->execute();

My current issue that I haven't been able to circumvent is in the event the user needs to update/change information in the fields on a node that has already been submitted, the validator is still run on that ID field and throws an error that this ID already exists - hence changes are not saved. I figure the easiest solution is to figure out if there's a way to remove the constraint property on the field only if the field is the same as it was previously. That way, if a user needs to edit that particular node, when they save the changes, the validator for the ID field doesn't throw an error.

  • 4
    Did you know there's already a core constraint for this? It's called UniqueField – Clive Jan 8 at 14:59
  • Hello Clive, I did not... I mean i figured there should be but it can be quite difficult looking for these specific api definitions, in my opinion. Thank you. I think this handles everything how I had wanted. – ovp Jan 8 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.