Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
I have a field of type entity reference. Number of values is set to unlimited. But I want to make sure that a user does not reference the same entity twice. I am sure this is a use case that has been encountered before. What is the best way to accomplish this?
Comments
Comment #1
awm CreditAttribution: awm commentedAnyone?
Comment #2
aimevpI'm also in need of this functionality. Have you found a solution yet?
The second part of my problem is that I would like to accomplish this by making a multi-select list where only the entities appear that haven't been referenced yet. Is there someone who has an idea how to accomplish this?
Comment #3
awm CreditAttribution: awm commentedOk, If your entity reference field is on a Node entity (Content type) you can use unique field module. It will still show referenced entities in the options but will fail validation, meaning will not let you save it.
Unique field does not work, or at least did not form me, when the entity reference field is on the User or entities other than node.
HTH
Comment #4
jonhattanyou can use entityreference_view_widget. See the project page for an explanation for "Exclude" items.
Comment #6
AaronBaumanjonhattan:
ERVW doesn't solve the problem, only hides it.
Has anyone found a way to enforce uniqueness in a multi-valued ER field?
Comment #7
AaronBaumanAlso, re Unique Field:
I would rather not have user's input fail validation when they choose duplicate values.
Rather, I would rather have a processing function that unique-ifies the values on submit.
Comment #8
Vacilando CreditAttribution: Vacilando as a volunteer commentedSearching for the same, I've spotted this: https://www.drupal.org/project/entityreference_unique -- perhaps of help.
Comment #9
frobThe afore mentioned entity reference view widget module from #4 also enforces this behavior. There are two settings; Pass ids back to view so they are excluded from the select view, and Allow the entity reference field to have the same item added twice.
Comment #11
ahmad1987 CreditAttribution: ahmad1987 commentedThis problem solved for drupal 7 not 8
so who have the solve for D8