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?

CommentFileSizeAuthor
#8 issue_with_default_value.jpg30.05 KBnagy.balint
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

nagy.balint created an issue. See original summary.

mably’s picture

Thanks 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:

  • Produce multiple field deltas from a single form element (like core's options_buttons widget does for entity reference fields), or
  • Serialize the selected values into a single text string (less clean, breaks filtering/sorting).

The 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 #states API shows the text input only when _custom_value is 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.

nagy.balint’s picture

Thank 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.

mably’s picture

Good point about cardinality detection. Adding checkboxes as a third select_type option (alongside select and radios) is cleaner than a separate widget. The widget settings form could use #states or 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's OptionsWidgetBase handles it: by overriding formMultipleElements() to render one element instead of one per delta.

So the approach would be:

  1. Add checkboxes to the select_type options in settingsForm().
  2. Override formMultipleElements() when select_type === 'checkboxes' to render a single set of checkboxes + one "Other" text field.
  3. In massageFormValues(), expand checked values into individual deltas.
  4. In setFormElementDefaultValues(), aggregate existing deltas to determine which boxes to pre-check.

I'll look into implementing this.

mably’s picture

Status: Active » Needs review

@nagy.balint could you have a look at this issue's MR please and see if it solves your problem?

mably’s picture

Summary of changes

Added checkboxes as a third select_type option (alongside select and radios) for all 4 widgets. This allows selecting multiple predefined values at once on multi-value fields.

How it works

  • When select_type === 'checkboxes', the widget's handlesMultipleValues() returns TRUE, telling Drupal to call formElement() once (not per delta) and merge the element flat (no delta wrapper).
  • A single #type => 'checkboxes' element renders all options, including an optional _custom_value ("Other") checkbox.
  • The "Other" text field appears/hides via #states based on the _custom_value checkbox being checked.
  • massageFormValues() expands checked values into individual storage deltas.
  • The checkboxes option is only available when field cardinality is greater than 1 or unlimited.

Files modified

  • src/WidgetHelper.php — Added checkboxes to settingsForm() options, adapted createSelectField(), added 5 new methods: setCheckboxesDefaultValues(), checkboxesFieldAssignStates(), checkboxesFormattedFieldAssignStates(), stringFieldMassageCheckboxesValues(), formattedTextFieldMassageCheckboxesValues().
  • src/Plugin/Field/FieldWidget/SelectStringTextfieldWidget.php — Added handlesMultipleValues(), branching in formElement(), afterBuild(), and massageFormValues().
  • src/Plugin/Field/FieldWidget/SelectStringTextareaWidget.php — Same as above, plus appendItem() guard to prevent null access in core's StringTextareaWidget.
  • src/Plugin/Field/FieldWidget/SelectFormattedTextfieldWidget.php — Same pattern for formatted text fields.
  • src/Plugin/Field/FieldWidget/SelectFormattedTextareaWidget.php — Same as above, plus appendItem() guard for core's TextareaWidget.
  • README.md — Mentioned checkboxes support, removed duplicate Installation section.

Test coverage added

  • Unit tests (44 tests) — tests/src/Unit/WidgetHelperTest.php: covers all WidgetHelper methods including checkboxes-specific ones.
  • Kernel tests (16 tests) — tests/src/Kernel/SelectTextValueWidgetKernelTest.php: widget integration with Drupal's field system, plugin discovery, form element building, default values, handlesMultipleValues(), config schema.
  • Functional tests (11 tests) — 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).

nagy.balint’s picture

StatusFileSize
new30.05 KB

It 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.

issue with default value

mably’s picture

Should be fixed. Could you give it another try?

nagy.balint’s picture

Seems to be fine!

Thank you!

mably’s picture

Status: Needs review » 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.

  • mably committed 0b1721c3 on 2.0.x
    feat: #3577793 select_or_other checkboxes support
    
    By: nagy.balint
    By:...

Status: Fixed » Closed (fixed)

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