Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
Currently you can refer to same entity multiple times in a multivalue reference field. We need to validate this and display an error when user mistakenly duplicated the reference.
Proposed resolution
To add a second option to Reference validators to unvalidate references that were added twice, by adding new constraint for that.
Comment | File | Size | Author |
---|---|---|---|
#10 | 2825868-10.patch | 15.57 KB | LOBsTerr |
#10 | 2825868-9-10.txt | 10.55 KB | LOBsTerr |
#9 | interdiff-2825868-6-9.txt | 722 bytes | LOBsTerr |
#9 | 2825868-9.patch | 5.93 KB | LOBsTerr |
#6 | interdiff.txt | 6.6 KB | pfrenssen |
Comments
Comment #2
sdstyles CreditAttribution: sdstyles at FFW commentedComment #3
sdstyles CreditAttribution: sdstyles at FFW commentedComment #4
pfrenssenAwesome patch, thanks for posting this!! It's exactly what I am looking for!
I had a quick code review, found some small remarks. I also tried the patch but I didn't see the option to enable this constraint appearing in the field settings. I'll have a closer look at what's causing that.
Since this is a visitor facing error message we should avoid using technical terms. A visitor does not know what entities or references are, and they haven't memorized entity IDs. They are just trying to invite people to the wedding and don't realize they added Auntie Caroline twice.
Let's make this a bit more friendly like "The 'invite' field should not have any duplicate values".
This is not correct, it checks whether there are duplicates, not whether the references are valid.
Instead of refining the class of the constraint through an inline type hint, we could also verify that it is of the correct type and throw an exception if it isn't.
How can
$value
not be set if it is passed in as an argument?It is probably better to use
empty()
here.Same here,
$entity
has been set on the preceding line, so!isset($entity)
will always return false here.This code is duplicated.
Comment #5
pfrenssenHad a deeper look, found two more issues.
This is why I'm not seeing the checkbox in the field configuration, I'm referencing a custom Business entity to a User entity, in my case the two types are not the same.
This part is causing the duplicate check to be skipped when I'm adding a new entity that has a duplicate reference.
Comment #6
pfrenssenWent ahead and addressed my remarks.
Comment #7
pfrenssenThis is also missing some test coverage.
Comment #9
LOBsTerr CreditAttribution: LOBsTerr commentedI have found a small bug. When for the entity reference field we have setting "Create referenced entities if they don't already exist" enabled. We will face the next warning, which breaks the ajax:
It happens, because the entity doesn't exist yet and as result target_id is empty.
I wanted to check these values for duplications also, but then I realized that this validation checks only existing entities. If we want avoid duplications when we create entities, we should use other solution like this https://www.drupal.org/project/taxonomy_unique. In addition each entity use different fields to search entity.
I have added a fix which filter out the empty values.
Comment #10
LOBsTerr CreditAttribution: LOBsTerr commented@pfrenssen I have added the functional and kernel tests. Some of the test for CircularReferenceConstraintValidator were broken. I fixed them also. Can you please check it ?
Comment #11
LOBsTerr CreditAttribution: LOBsTerr commentedComment #12
markdcI'm looking for this feature in a multi-value Paragraph that contains a reference field. Since that would involve integrating with the Paragraphs module, I'll create a new feature request.
Comment #13
markdcBy the way, I tested the above patch and it works. When trying to add the same entity more than once I get a validation error that said entity "has been entered multiple times."
Suggested change: the option should only appear on reference fields set to limit of 2 or more values. It's currently showing on 1-value fields.
Comment #14
alexpottWe can improve this code by using #access instead of the if.
@fatmarker that's good idea. Let's do that in a follow-up.
Comment #16
alexpottComment #18
FrancewhoaThanks all for your contributions with this port :)
Today I set the related ticket to fixed