Problem/Motivation
In the current UI a user can add a huge number of options in a list, and choose either a Select Box or Radio buttons regardless of it's UX.
User interface guidelines like from MS, Apple, Gnome etc give usually quite clear suggestions on this:
- Radio buttons: less than 5 choices
- Select Box: 6 to 10 options
- Scrollable list: 11 or more options or
See reference here.
Other sources:
- https://www.nngroup.com/articles/listbox-dropdown/
- https://uxpickle.com/dropdown-vs-radio-button-vs-checkboxes-which-one-to...
- https://joechiang.com/post/when-use-radio-buttons-select-dropdowns
Proposed resolution
6 years ago, Drupal Commerce added an element for entity references. It adapts to the available options: if there are 7 or more options, a select box is used instead.
https://git.drupalcode.org/project/commerce/-/blob/8.x-2.x/src/Element/E...
I remember bojanz researched and found 7 was a common pattern.
(@mglaman)
A lot of people building forms (either on the admin UI like node forms or final user forms like webforms or entity references) won't know this rule, or won't even be able to know how many items will be available when building the form. Providing an option that automatically converts large radio/checkbox option would improve usability.

Remaining tasks
User interface changes
A new form element "dynamic_options" has been added to address this feature request.
To make it available to end users a new widget declared in class Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsDynamicWidget has been created.
This widget can be used used in both options and entity reference fields, like the existing "Select list" and "Check boxes/radio buttons" ones.
Site admins only need to configure the entity form mode like in the screenshot:

And then, the entity form dynamically would look like the one in the screenshot depending o nthe field and widget settings:

API changes
Data model changes
Release notes snippet

| Comment | File | Size | Author |
|---|---|---|---|
| #26 | 3265408-nr-bot.txt | 90 bytes | needs-review-queue-bot |
| #23 | full-widget.gif | 271.59 KB | plopesc |
| #18 | widget-ux-changes-idea.png | 28.87 KB | ckrina |
| #17 | Screenshot from 2022-11-11 20-47-50.png | 41.44 KB | benjifisher |
| #13 | form-widget.png | 30.05 KB | plopesc |
Issue fork drupal-3265408
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:
- 10.0.x
changes, plain diff MR !2518
- 3265408-provide-an-element
changes, plain diff MR !2939
Comments
Comment #2
andypostAs it targeted for D10 I think it could use typed data to detect length instead of render array guessing, so added as related #3249600: Add support for PHP 8.1 Enums as allowed values for list_* field types
Comment #5
plopescHello,
Created a MR for this task that provides a new Form element called "dynamic_options" that depending on the number of items behaves as a set of checkboxes/radiobuttons or a select element.
The number of options to determine one behavior or the other can be determined by the
#select_thresholdproperty.Attaching an example of the behavior of this form element and the source code necessary to generate that form.


This MR will require tests, but here I'm presenting a first candidate to move on one direction or the other depending on the feedback given.
Thank you!
Comment #6
plopescComment #8
ckrinaThanks all! I think this can be a great UX improvement for Drupal.
From the current work I'd miss a way to test it: would you mind adding a Widget too? I would love to test this via the UI and be able to see it "in action" so we can convince everybody that this is needed. Also, if you could add some suggestion for testing steps once it is done it'd be great to get people's +1.
Comment #9
plopescHi @ckrina
Added the widget as you suggested to allow site admins to use this new form element from the Drupal UI.
Issue description has been updated with some instructions and screenshots. Please let me know if you think that anything else can be added.
Thank you!
Comment #10
ckrinaWe've discussed this during the weekly UX meeting #3318005: Drupal Usability Meeting 2022-11-04 and everybody agreed on this being ok with this improvement.
On the other side, they suggested to change the following copy from the UI:
And also to add a suffix to the advanced form where you can choose the amount of items as "options".
So removing the "Needs usability review" tag.
Comment #11
ckrinaComment #12
plopescThank you very much for the quick feedback.
Form changes have been already addressed.
Comment #13
plopescComment #16
benjifisherWe discussed this issue at #3318005: Drupal Usability Meeting 2022-11-04. (See Comment #10.) That issue will have a link to a recording of the meeting.
For the record, the attendees at the usability meeting were @AaronMcHale, @anmolgoyal74, @benjifisher, @ckrina, @kae76, @rkoller, @shaal, @simohell, and @worldlinemine.
Comment #17
benjifisherDuring the usability meeting, we agreed that adding a new form widget, as in the current patch, is the right thing to do. Thinking about it some more, I am not so sure.
The new widget has the setting "Switch to Select at". If we set that to 0 (or -1 or whatever is interpreted as "never") then we could replace the existing checkboxes/radio widget with the new one and it would not be disruptive. We would have to decide what the default behavior will be for newly added fields.
The advantage of this approach is that we do not add an extra option to the list of available widgets. We do not have to worry about putting "select" at the end of the widget name or at the beginning. (There are arguments for both, and we discussed it at the usability meeting.) Less is more! Here is a screenshot using the current feature branch for this issue:
Looking at that makes me wonder:
I am not suggesting that we do anything about those two questions on this issue, but if anyone wants to look for existing issues and create new ones if needed, I will be grateful.
In fact, why not replace both the existing Checkboxes/radio widget and the Select widget with the new, configurable one? Use a setting of -1 to mean never use Select and a setting of 0 to mean always use Select.
I am setting the status to NW for a response. The response could be that you do not like my ideas, or it could be an updated MR to implement them.
Comment #18
ckrinaI really like the idea of improving something that we already have instead of creating something new! I wouldn't go as far as merging the Checkboxes/radio widget with the Select one though.
So as @benjifisher said, I would try to improve the UX of the existing Checkboxes/radio by adding the option to switch automatically to a Select. I'd also hide it behind the advanced options/widget settings, and a checkbox disabled by default that would activate the feature with the 7 as the default number (and an input field to change it).
Any thoughts on the implementation perspective and its repercussions?
Comment #19
plopescI agree that updating the existing widget instead of adding a new one is a good idea, but I believe there are some secondary effects we should pay attention to before making the decision.
Adding new settings to existing widget
Setting these properties by default to behave like the existing widget would not be a big deal, and probably it would not require too much code. To be more solid, we might need to add an update hook to update all the existing instances of the widget. Then export the results and update the corresponding form display yml files affected.
It would imply to set by default settings to behave like
checkboxes/radio buttonscurrent widget by default and explicitly set the dynamic behavior afterwards where considered necessary.Breaking existing contrib & custom modules that extend the current widget
Given that besides of the new extra widget settings, it would make use of this new dynamic_options form element, widgets created existing the current widget class could be affected.
Not sure if this could be even considered as a Breaking BC change.
To alleviate this, we could add this new widget, and modify the existing one to extend it. Then deprecate the existing one and get rid of it in next major release.
I think those are my main concerns from the technical point of view regarding this topic. Any feedback from someone with a better knowledge of the release cycle would be much appreciated to validate the situations explained above.
Thanks!
Comment #20
alexpottI think as long as when we change the existing widget we make the new behaviour not how existing configurations of the widget work we'd be okay. As plugin implementation we don't support the BC policy on
\Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsButtonsWidget...But that's actually also interesting - the class name is not great if we make it capable of being a select so maybe we'll need to rename the class here too - which would mean deprecating the current class name. And then there's the question of the plugin ID... it's
options_buttonswhich also is not great.So perhaps the best thing to do is:
Comment #21
plopescThank you for your feedback here @alexpott
Let me try to add some questions to your your suggested steps, to confirm that we are in the same page:
options/\Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsWidgetoptions_buttons&options_selectinstances to the new widget?Thanks!
Comment #22
andypostComment #23
plopescHello,
I have been working on the proposal to provide a widget that allows to force the element or allow the dynamic conversion depending on the number of options.
This is the current status of the widget settings form:

Feedback is welcome!
Comment #25
gauravvvv commentedComment #26
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.