I'm finding that if I have the widget mode set to 'Edit selection' and my field cardinality is set to 1, the 'Selections' in the modal can still display multiple files. I would expect that this not be the case.

It looks like the validation on the server doesn't really check this before just doing an array_merge to put the new selection in with the existing selections.

It is likely that the more general cardinality case wants to be solved here (i.e. cardinality > 1 and not infinite), but that's a different set of problems once you add in the prepend/append mode options etc. So I'm going to keep this ticket on track for this particular single selection problem.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

amoebanath created an issue. See original summary.

amoebanath’s picture

Assigned: amoebanath » Unassigned
Status: Active » Needs review
StatusFileSize
new886 bytes

Patch!

Checks if there should be a cardinality validation of 1, and, if so, replaces the previous selection with the new selection (which has already been validated as a single selection).

amoebanath’s picture

StatusFileSize
new1.57 KB
new1.04 KB

This seems to have brought up another issue a little further along. An easy fix, by the look of things.

amoebanath’s picture

Assigned: Unassigned » amoebanath

Hmm. Maybe this isn't the quick fix I thought.
The final selection value doesn't seem to save...

amoebanath’s picture

StatusFileSize
new1.71 KB

Gotcha. When you submit the modal, the selections don't quite follow the same routes. So we just need to handle that scenario by checking on $entities before handling that value.

marcoscano’s picture

Status: Needs review » Postponed (maintainer needs more info)
Issue tags: +D8Media

I have tried to manually reproduce the failing scenario you mention, but I am unable to do it with the current -dev.

Could you please indicate if this issue still exists, and if so, provide steps to reproduce it?

Thanks!

rp7’s picture

Status: Postponed (maintainer needs more info) » Active
StatusFileSize
new1.06 KB

@marcoscano
According to me this issue still exists.
Pre-requisites:

  • Have a single value field
  • Entity browser with views widget & multi step selection

If you select multiple items & try to add it to the selection, you will get an error message that only 1 item may be selected = OK.
Selecting 1 item & adding it to the selection works, but you can repeat this infinitely. Multiple items will be in the selection, while the field itself can only reference 1. If you submit the selection, only the first item is applied.

I see multiple solutions

  1. Provide an error message when you try to add an item to a selection while the cardinality is already reached
  2. Leave widget selection as is, but validate the selection cardinality when submitting the entity browser.
  3. Allow to add as many items as you wish to the selection, but validate the selection cardinality when submitting the entity browser.

I couldn't get patch in #5 to work. Patch attached goes for solution number 2 (see above). I'm wondering if the widget validators can't be used to validate the selection as well...

piotr pakulski’s picture

Assigned: amoebanath » Unassigned

mdranove made their first commit to this issue’s fork.

mdranove changed the visibility of the branch 2825730-issue-with-selections to hidden.

mdranove changed the visibility of the branch 2825730-issue-with-selections to active.

mdranove’s picture

Status: Active » Needs review

I had this same issue. patch in #7 works well, looks like a clean solution. Created MR and moving to NR. I think if we want to do some js work that should be done in a separate issue.

anybody’s picture

@mdranove thanks! Could you check if #3177204: It's possible to overflow the number of allowed items with edit selection is a duplicate or should also be fixed by the MR?
Then we could push (both) things forward. Thank you!

anybody changed the visibility of the branch 2825730-issue-with-selections to hidden.

anybody’s picture

Version: 8.x-1.x-dev » 8.x-2.x-dev
anybody’s picture

mdranove’s picture

Hi @anybody.

Yes looks like #3177204: It's possible to overflow the number of allowed items with edit selection is a duplicate issue, although the proposed resolution is different than what is done here. I tried the patch provided in the ticket and it throws an exception on my Drupal 10.2,

I recommend moving forward with this MR.

benstallings’s picture

Status: Needs review » Needs work

Needs merge conflicts resolved.

benstallings’s picture

Assigned: Unassigned » benstallings
benstallings’s picture

Assigned: benstallings » Unassigned
Status: Needs work » Needs review
anybody’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Thanks!! I think it would make a lot of sense to have tests around this now.

benstallings’s picture

Assigned: Unassigned » benstallings
benstallings’s picture

Assigned: benstallings » Unassigned
Status: Needs work » Needs review

New test: CardinalityTest::testSelectionDisplayCardinality().

Targets the new code path specifically rather than retesting the widget-level validator. Setup: cardinality-2 entity_reference field on article, wired to the bundle_filter browser (multi_step_display) in SELECTION_MODE_APPEND. The test selects three nodes one-at-a-time — each press is a single-entity batch that the widget-level Cardinality validator passes, but selected_entities grows to 3. Pressing "Use selected" then has to be rejected by SelectionDisplayBase::validate(); nothing else in the validation stack can catch it. Asserts the "You can only select up to 2 items" error.

anybody’s picture

Status: Needs review » Needs work

Thanks!! Tests are currently failing

benstallings’s picture

Assigned: Unassigned » benstallings
benstallings’s picture

Assigned: benstallings » Unassigned
Status: Needs work » Needs review
anybody’s picture

Status: Needs review » Reviewed & tested by the community

Thank you @benstallings! LGTM!

anybody’s picture

Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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