Problem / Motivation
I have enabled the Entity Browser with version 8.x-2.2. I just added the one result to node and second time the same content can be added. This is duplicate is added because of below code.

Drupal.entityBrowser.updateEntityIds = function (existing_entities, selected_entities, selection_mode, cardinality) {
    var combined_entities;

    if (selection_mode === 'selection_edit') {
      // Propagate new selected entities.
      combined_entities = selected_entities;
    }
    else if (selection_mode === 'selection_prepend') {
      // Prepend selected entities to existing list of entities.
      combined_entities = selected_entities.concat(existing_entities);
    }
    else {
      // Append selected entities to existing list of entities.
      combined_entities = existing_entities.concat(selected_entities);
    }

    // Having more elements than cardinality should never happen, because
    // server side authentication should prevent it, but we handle it here
    // anyway.
    if (cardinality > 0 && combined_entities.length > cardinality) {
      combined_entities = combined_entities.slice(0, cardinality);
    }

    return combined_entities.join(' ');
  };

Please review my patch.
Thanks

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

iyyappan.govind created an issue. See original summary.

iyyappan.govind’s picture

Issue summary: View changes
iyyappan.govind’s picture

Priority: Normal » Major
sumitmadan’s picture

Assigned: Unassigned » sumitmadan

The patch solves the duplicate reference for the append/prepend selections. But in the case of multistep selection, it still an issue. I am trying to work on it.

sumitmadan’s picture

Assigned: sumitmadan » Unassigned
Status: Active » Needs review
FileSize
1.81 KB
1.34 KB

I have updated the patch. Added an interdiff of the patch as well.

Hardik_Patel_12’s picture

Assigned: Unassigned » Hardik_Patel_12
Hardik_Patel_12’s picture

Kindly follow a new patch. Added an interdiff of the patch as well.

Hardik_Patel_12’s picture

Assigned: Hardik_Patel_12 » Unassigned
iyyappan.govind’s picture

Hi Sumit & Hardik,

Reg test failers: We need to pass the entity index with the $selected_entities variable. But I can see that your patches are setting entity as key in the $selected_entities variable. That is why test is getting failed. Any thoughts ? This needs maintainer review. So adding the issue tags.

DiDebru’s picture

#7 does not work on IE11.
Maybe related to that issue https://www.leereamsnyder.com/blog/new-skool-uniq-in-internet-explorer

DiDebru’s picture

This works for me.

gwvoigt’s picture

This patch works for me on Firefox 86.0 (64-bit), but fails on Chrome Version 88.0.4324.192 (Official Build) (x86_64). Is it a duplicate of https://www.drupal.org/project/entity_browser/issues/2561411 ?

CRZDEV’s picture

Adding new patch using _.unique insead new Set (@see drupal.org/project/entity_browser/issues/2561411) to prevent possible browser incompatibilities and also note that function where is added is "Drupal.entityBrowser.updateEntityIds" instead "Drupal.entityBrowser.selectionCompleted" so this will manage duplications even with selected entities (previous patch from this issue just prevent duplications when new references are added but old ones).

Widget table seems not being able to manage duplications; duplications are "hidden" but duplicated reference is mantained, so this is to fix duplications assuming that behaviour by default, in any selection mode.

DuneBL’s picture

I confirm #13 is working.
Note for those who have problem to make it work: make sure that your browser (and of course Drupal) doesn't use the cached JS.
In other words, on my setup, clearing the caches and restarting apache was not enough: I had to disabled the JS cache in the dev panel (f12) of my chrome browser.

Gaurav_drupal’s picture

#13 works
+1 to clear browser cache

sharma.amitt16’s picture

#13 looks good and working as expected for me.

Tested on below browsers.

Safari: Version 14.1.1 (16611.2.7.1.4)
Firefox: 92.0 (64-bit)
Chrome: Version 93.0.4577.82 (Official Build) (x86_64)
Microsft Edge: Version 93.0.961.52 (Official build) (x86_64)

Thanks for submitting the patch. +1 for RTBC.

Nicolas S.’s picture

#13 works for me

delacosta456’s picture

Hi #13 works for me too after clearing browser cache and many thanks for the work.
However i was thinking if it is possible to Exclude already selected entities from listed ones for selection
something that was possible in this modulehttps://www.drupal.org/project/entity_reference_views_select

chrisfromredfin’s picture

Status: Needs review » Reviewed & tested by the community

I'm also using this and it's a very effective and small patch. Curious if maintainer thinks current functionality is a feature or not, and whether or not this behavior should be optional / controlled with a checkbox? But this is the behavior I'd expect.

apaderno’s picture

oleg chemerys’s picture

The patch from #13 is no longer working as of Drupal 9.4.0 release, since Underscore library is now deprecated. See notes here https://www.drupal.org/node/3273118. I have re-rolled the patch using new Set as it is now widely supported.

samaphp’s picture

Patch #21 Tested on Drupal 9.5.9
It's simply working well.

I liked that it is just a simple line of code.
Hope it gets approved soon. thanks.

apaderno’s picture

Title: Avoid Duplicate references » Avoid duplicated references

Dave Reid’s picture

Status: Reviewed & tested by the community » Fixed

Committed to 8.x-2.x. I'm a little curious, what if I did want to select the same entity more than once? Should this be configurable? Or is it generally undesired?

Status: Fixed » Closed (fixed)

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