The old Drupal 7 select_or_other module supported checkboxes option, where instead of rendering the widget as a multi value select field, the user can select multiple values at once.
Would it be difficult to implement for this module?
| Comment | File | Size | Author |
|---|---|---|---|
| #8 | issue_with_default_value.jpg | 30.05 KB | nagy.balint |
Issue fork select_text_value-3577793
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
Comment #2
mably commentedThanks for the feature request!
I looked into what it would take to add checkboxes support. Here is a summary of the challenges:
Storage model
The current widgets are designed around a single value per field delta — one select/radio choice maps to one stored text value. Checkboxes imply multiple simultaneous selections, which means the widget would need to either:
options_buttonswidget does for entity reference fields), orThe first approach is the correct one, but it requires the field to be configured with unlimited cardinality (or at least > 1).
Interaction with the "Other" custom value
With select/radios, the custom value field is mutually exclusive with the predefined options — the
#statesAPI shows the text input only when_custom_valueis selected. With checkboxes, the custom value could coexist with other checked options, so the visibility logic and the value massaging would need to be reworked.Default value handling
The current
setFormElementDefaultValues()reads a single$items[$delta]->value. A checkboxes widget would need to aggregate values across all deltas to determine which boxes to pre-check.Conclusion
It is doable but not trivial — it would likely be a new widget variant rather than a setting on the existing widgets, since the form element structure, state handling, and value massaging are fundamentally different for multi-value selection.
Comment #3
nagy.balint commentedThank you for the quick reply!
Since this module is about the text fields only,
it seems to me that this checkboxes widget would not make sense for single value cardinality.
So not sure if the widget settings could detect the cardinality and provide the options to switch to checkboxes if it is multi value.
But of course I understand that code quality wise a separate widget would likely look better,
but then we would have a widget which is not usable on certain cardinality, at least to set it for a single cardinality field is not very usable then.
Not sure what is the best approach.
Comment #4
mably commentedGood point about cardinality detection. Adding
checkboxesas a thirdselect_typeoption (alongsideselectandradios) is cleaner than a separate widget. The widget settings form could use#statesor validation to only allow checkboxes when the field cardinality is greater than 1.The main implementation challenge is that the current architecture calls
formElement()once per delta — each delta gets its own select/radio + text field pair. Checkboxes need a single set of checkboxes that produces multiple values. This is how core'sOptionsWidgetBasehandles it: by overridingformMultipleElements()to render one element instead of one per delta.So the approach would be:
checkboxesto theselect_typeoptions insettingsForm().formMultipleElements()whenselect_type === 'checkboxes'to render a single set of checkboxes + one "Other" text field.massageFormValues(), expand checked values into individual deltas.setFormElementDefaultValues(), aggregate existing deltas to determine which boxes to pre-check.I'll look into implementing this.
Comment #6
mably commented@nagy.balint could you have a look at this issue's MR please and see if it solves your problem?
Comment #7
mably commentedSummary of changes
Added checkboxes as a third
select_typeoption (alongside select and radios) for all 4 widgets. This allows selecting multiple predefined values at once on multi-value fields.How it works
select_type === 'checkboxes', the widget'shandlesMultipleValues()returns TRUE, telling Drupal to callformElement()once (not per delta) and merge the element flat (no delta wrapper).#type => 'checkboxes'element renders all options, including an optional_custom_value("Other") checkbox.#statesbased on the_custom_valuecheckbox being checked.massageFormValues()expands checked values into individual storage deltas.Files modified
src/WidgetHelper.php— Added checkboxes tosettingsForm()options, adaptedcreateSelectField(), added 5 new methods:setCheckboxesDefaultValues(),checkboxesFieldAssignStates(),checkboxesFormattedFieldAssignStates(),stringFieldMassageCheckboxesValues(),formattedTextFieldMassageCheckboxesValues().src/Plugin/Field/FieldWidget/SelectStringTextfieldWidget.php— AddedhandlesMultipleValues(), branching informElement(),afterBuild(), andmassageFormValues().src/Plugin/Field/FieldWidget/SelectStringTextareaWidget.php— Same as above, plusappendItem()guard to prevent null access in core'sStringTextareaWidget.src/Plugin/Field/FieldWidget/SelectFormattedTextfieldWidget.php— Same pattern for formatted text fields.src/Plugin/Field/FieldWidget/SelectFormattedTextareaWidget.php— Same as above, plusappendItem()guard for core'sTextareaWidget.README.md— Mentioned checkboxes support, removed duplicate Installation section.Test coverage added
tests/src/Unit/WidgetHelperTest.php: covers all WidgetHelper methods including checkboxes-specific ones.tests/src/Kernel/SelectTextValueWidgetKernelTest.php: widget integration with Drupal's field system, plugin discovery, form element building, default values,handlesMultipleValues(), config schema.tests/src/Functional/SelectTextValueWidgetFunctionalTest.php: end-to-end via node forms for select, radios, and checkboxes modes across string, string_long, and text_long field types.All 71 tests pass (325 assertions).
Comment #8
nagy.balint commentedIt works fine!
I noticed an issue though, maybe not a big one.
Steps:
- Set a default value for the field
- Set the field as multi value.
- Select the Checkboxes option in the widget.
Now when selecting the custom Other checkbox the textbox will show the default value. But in the case of multi value it should show an empty value, since then we can have both a selected value and an Other value.
Comment #9
mably commentedShould be fixed. Could you give it another try?
Comment #10
nagy.balint commentedSeems to be fine!
Thank you!
Comment #11
mably commented