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
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
Comments
Comment #2
iyyappan.govindComment #3
iyyappan.govindComment #4
sumitmadan CreditAttribution: sumitmadan at QED42 commentedThe 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.
Comment #5
sumitmadan CreditAttribution: sumitmadan at QED42 commentedI have updated the patch. Added an interdiff of the patch as well.
Comment #6
Hardik_Patel_12 CreditAttribution: Hardik_Patel_12 commentedComment #7
Hardik_Patel_12 CreditAttribution: Hardik_Patel_12 commentedKindly follow a new patch. Added an interdiff of the patch as well.
Comment #8
Hardik_Patel_12 CreditAttribution: Hardik_Patel_12 commentedComment #9
iyyappan.govindHi 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.Comment #10
DiDebru#7 does not work on IE11.
Maybe related to that issue https://www.leereamsnyder.com/blog/new-skool-uniq-in-internet-explorer
Comment #11
DiDebruThis works for me.
Comment #12
gwvoigtThis 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 ?
Comment #13
CRZDEV CreditAttribution: CRZDEV at Metadrop commentedAdding 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.
Comment #14
DuneBLI 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.
Comment #15
Gaurav_drupal#13 works
+1 to clear browser cache
Comment #16
sharma.amitt16 CreditAttribution: sharma.amitt16 as a volunteer and at Srijan | A Material+ Company for Drupal India Association commented#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.
Comment #17
Nicolas S. CreditAttribution: Nicolas S. commented#13 works for me
Comment #18
delacosta456 CreditAttribution: delacosta456 commentedHi #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
Comment #19
chrisfromredfinI'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.
Comment #20
apadernoComment #21
oleg chemerys CreditAttribution: oleg chemerys commentedThe 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.
Comment #22
samaphpPatch #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.
Comment #23
apadernoComment #25
Dave ReidCommitted 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?