Remove "_none" option in options_select widget with slim_select.

Created on 12 October 2023, 9 months ago
Updated 13 October 2023, 9 months ago

Problem/Motivation

The 'options_select' widget (class OptionsSelectWidget) adds an option ['_none' => '- None -'] to a multiple-value select element, if the field is optional.
(single-value fields are not relevant for this module, we don't need to discuss them)
The logic is in OptionsSelectWidget::getEmptyLabel().

Without slim_select, the "None" option is an easy way to reset the multiple-value select element.

With the slim_select applied, the "None" option is useless and confusing.

Steps to reproduce

Create a multiple-value taxonomy term reference field, not required.
Choose "Select list" widget in the form.
Add php code to activate slim_select for all selects or for this specific select.

Visit the form with the field.

It is now possible to add a "None" item to the selection.

Proposed resolution

We should not remove the '_none' option on php side, because we want the widget to work 1:1 as in core if slim_select is not active.
We also should not blindly remove '_none' from every select element, because there could be cases where '_none' is a legitimate value.

Instead, we should remove the '_none' option on client side for select elements that are generated by the OptionsSelectWidget.

To do this, we need to send a value to the javascript, possibly in a data attribute.

Given that slim_select has to be activated with php code which is not controlled by slim_select module, we can also ask that this 3rd party php code should activate the removal of the '_none' option.

We could add another value in data-drupal-slim-select, but this could create conflicts if in the future we want to support more settings from slim_select library.

Remaining tasks

User interface changes

API changes

Data model changes

πŸ“Œ Task
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡©πŸ‡ͺGermany donquixote

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

Comments & Activities

  • Issue created by @donquixote
  • πŸ‡©πŸ‡ͺGermany donquixote

    I know of one specific integration that uses hook_element_info_alter() to register a '#process' callback on 'select' to set
    $element['#slim_select'] = [].

    This will overwrite all other settings in $element['#slim_select'].
    So if we add more settings in '#slim_select' property, these would be wiped when the '#process' callback runs.

  • πŸ‡©πŸ‡ͺGermany donquixote

    So, turns out this is quite easy in custom code.
    The difficult part is proper API design that we don't regret in the future.

Production build 0.69.0 2024