Supporting cardinality (i.e. preventing how many items can be selected with JS)

Created on 21 October 2017, almost 8 years ago
Updated 1 September 2025, about 1 month ago

I just got started with Entity Browser and was super happy to find File Browser module, great work!
Before including it in my distribution I wanted get rid of some critical UX problems, i.e. you can keep selecting files if the cardinality set on the field won't support it. Thankfully I found this problem was already solved in a collaboration of both the Enhanced Entity Browser module and Lightning Media. I ported over their code to work with File Browser.
I also considered creating a patch for Entity Browser but as of now this code needs to know row classes of the views used as well as make sure the Entity Browser does not use Multi-step selection. I hope this code can later join the Entity Browser.

Behaviors added by Patch:

Cardinality = 1
User can select only 1 file. Clicking a new file results in deselecting of selected file. You can double click on a file as a shortcut to select and submit it.

Cardinality = between 1 and unlimited
Users can select multiple files until cardinality is reached. When cardinality equals count of selected files, all unselected files are disabled (look desaturated). Clicking selected files deselects them.

Cardinality = unlimited
You can select as many files as you like.

p.s. because without multi step form it is more important to clearly highlight selected files I incraesed the border size 1px on selected files and made the green color a bit brighter. (and matched the green color to the green check mark on Drupal core's status report page)

πŸ“Œ Task
Status

Postponed: needs info

Version

2.0

Component

Code

Created by

πŸ‡³πŸ‡±Netherlands jurriaanroelofs

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024