Hello.
I think that functionality for forbid to use duplicate entities in entityreference field with multiple values will be very useful.

I wrote patch. It will add simple checkbox into field settings. When "Forbid duplicates" is checked and you tryed to use duplicate entities in this field, you will get validation error.

forbid duplicates

Comments

eugene.ilyin’s picture

Sorry, it's wrong patch. Don't use it.

eugene.ilyin’s picture

Status: Active » Needs review
StatusFileSize
new2.1 KB

If you will approve my patch, please specify me as author of the commit.

amitaibu’s picture

What's the use case?

eugene.ilyin’s picture

For example:
We have entity reference field with multiple values for display related articles of main article. Will be good to avoid duplicates in this field automatically.

eugene.ilyin’s picture

What you think about it? I think it's very convenient.

eugene.ilyin’s picture

Any news?

eugene.ilyin’s picture

Hello.

Can you commit my patch please? It's very usefull.

melchior’s picture

Hot!

Please commit this patch.

It would be nice if there could be a silent mode that auto-removes the value instead of marking it red.

thx!

amitaibu’s picture

Status: Needs review » Postponed

This feature needs to be created against D8, and backported to D7. Anyway, I think it can live in contrib and not in ER-core.

eugene.ilyin’s picture

You suggesting me to create separate module for this little functionality?

amitaibu’s picture

> You suggesting me to create separate module for this little functionality?

Or try to push it in D8, and then we can backport it to D7.

eugene.ilyin’s picture

As I see entity_reference for D8 has been included into core. I should write patch for this module in core? I think that it will be very difficult and I will wait year, until it will be approved :(

eugene.ilyin’s picture

Issue summary: View changes

add tag for image

hedgefrog’s picture

I would find this very useful - could it be committed in D7 as it is?

eugen.ilyin doesn't seem willing to create it for D8?

eugene.ilyin’s picture

I asked in #12. Should I prepare this patch for d8 version of this project, or for entity_reference in D8 core?

eugene.ilyin’s picture

Please answer to me

manuelBS’s picture

By the way, there is a module for that: https://drupal.org/project/entityreference_unique

codesidekick’s picture

Issue summary: View changes

entityreference_unique doesn't export to Features and is pretty poor in terms of coding standards. I think this is something that should be included as part of the module.

shiraz dindar’s picture

I don't understand what the "Forbid Duplicates" entity reference field option is even for if it doesn't do anything without this patch. I too think this should be rolled into D7. It works for me.

toddwoof’s picture

Maybe this is a Feeds issue -- but neither this patch nor the module referenced in #16 works when importing content using Feeds. With "Forbid duplicates" checked (or the corresponding checkbox for the module), multiple imports results in multiple identical references in a given field.

rusticdoozy’s picture

Why not use a view which can produce a filtered list with no duplicates and then use the view as filter itself in entity reference field settings?

maxilein’s picture

Well using D8: by mistake I added the same title twice. Now I cannot save that nodes...

Drupal\Core\Entity\EntityStorageException: Unable to remove item at non-existing index. in Drupal\Core\Entity\Sql\SqlContentEntityStorage->delete() (line 718 of /core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

That is why this is a good option!

toddwoof’s picture

@rusticdoozy: That wouldn't allow new values to be imported.

kris77’s picture

@eugene.ilyin i tried your patch but same doesn't work with field collection.

This my scenario:

  • Entityform type: FORM1
    • In FORM1 i have a field collection field: FC1
    • In FC1 i have a entityreferencefield with autocomplete widget: ER1

In my ER1 i select checkbox" Forbid duplicates".

So, when i visit page to create my entityform type(FORM1)

  • I select entity in ER1 and click on field collection button "add more item",
  • Select the same entity and click on field collection button "add more item" but error not appear.
  • Click on SUBMIT button but error not appear and the form is submit.
Rafal Lukawiecki’s picture

I also have a use case for this feature. We are using entity references to create something similar to a table of content, and it would be useful to prevent a node from being added more than once to its "parent" (in terms of ToC).

The autocomplete widget in the entity reference module is very useful for us, but, unfortunately, it lists nodes that have already been referenced, and so allows for erroneous entries to be made. This feature would prevent those errors. Any plans to have this patch committed to the module?

xlin’s picture

StatusFileSize
new869 bytes

Attached patch to remove duplicates on save rather than validate. DO NOT use this patch if you want to inform your user that hey make a mistake.

xlin’s picture

StatusFileSize
new1.53 KB

Re-use the checkbox option provided by @eugene.ilyin.

joshua.boltz’s picture

Is there an existing Drupal 8 issue around this? I could also see this being a useful feature, as editors can easily add the same referenced entities in the multi-valued entity reference autocomplete widget.

It would be nice to either:
1) provide the user an error message when saving the entity that contains the ER field to tell them they added duplicates and they should fix it before the form can be saved
2) validation after entering a referenced entity that is already added to the field (instant feedback to the user)

delacosta456’s picture

hi
having this directly tied in Entityreference could be interested and reduce time wasting for workaround like building a views..

About workaround i think entityreference_view_widget and references_dialog are some modules that could help avoiding duplicate in multified of entityrefence ...

The entityreference_view_widget has a visual documentation giving more understanding HERE