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

awm’s picture

Anyone?

aimevp’s picture

I'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?

awm’s picture

Ok, 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

jonhattan’s picture

Version: 7.x-1.0-rc1 » 7.x-1.x-dev
Status: Active » Fixed

you can use entityreference_view_widget. See the project page for an explanation for "Exclude" items.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

AaronBauman’s picture

Status: Closed (fixed) » Active

jonhattan:
ERVW doesn't solve the problem, only hides it.
Has anyone found a way to enforce uniqueness in a multi-valued ER field?

AaronBauman’s picture

Also, 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.

Vacilando’s picture

Issue summary: View changes

Searching for the same, I've spotted this: https://www.drupal.org/project/entityreference_unique -- perhaps of help.

frob’s picture

Status: Active » Fixed

The 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.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

ahmad1987’s picture

This problem solved for drupal 7 not 8
so who have the solve for D8