Problem/Motivation

There are several WTFs with core's boolean_checkbox ("Single on/off checkbox") and options_buttons ("Check boxes/radio buttons") widgets:

  1. If you add a boolean field to a content type, configure it to have an "Allowed number of values" > 1, and let it use the default "Single on/off checkbox" widget in the form display, then when editing the content, the widget only presents a single checkbox (as advertised), and therefore, only allows you to save 1 value for the field, despite the field being allowed to have more.
    Screenshot of boolean field with unlimited allowed values using "Single on/off checkbox" widget:
  2. If you then change the form display to use the "Check boxes/radio buttons" widget, you get 2 checkboxes when editing the content ("Off" and "On"), even if the field is configured with an allowed number of values > 2.
    Screenshot of boolean field with unlimited allowed values using "Check boxes/radio buttons":

    Screenshot of boolean field with 1 allowed value using "Check boxes/radio buttons":

  3. The above is a special case of similar behavior for when the "Check boxes/radio buttons" is used for any of the List fields (e.g., "List (text)"). You only get as many checkboxes as the number of items in "Allowed values list" even if "Allowed number of values" is more than that.

All 3 of the above behaviors occur on Drupal 8, Drupal 7, and Drupal 6 (with CCK).

Proposed resolution

  1. The first problem can almost be fixed by adding a BooleanCheckboxWidget::isApplicable() that returns false for fields with cardinality > 1. However, in this case, WidgetPluginManager::getInstance() falls back to the default widget for the field type, and BooleanCheckboxWidget is the default widget for BooleanItem, so we'd need to resolve that somehow.
  2. The second problem could in theory be solved by adding a OptionsButtonsWidget::isApplicable() that returns false for boolean fields with more allowed values than 2.
  3. The above could be generalized to solve the 3rd problem by making OptionsButtonsWidget::isApplicable() return false for any field type with more allowed values than are in the allowed values list. But, is doing that desired?
  4. We may also want to explore adding new widgets to core that allow editing an arbitrary number of boolean items.

Remaining tasks

Define scenarios/use cases
Decide solution
Write patch
Add tests

User interface changes

?

API changes

?

Comments

larowlan’s picture

Comment alters the form to limit to one, perhaps we can create a solution they can share

damien tournoud’s picture

The field itself with a cardinality > 1 does make sense. It's the checkbox widget that doesn't.

But do we have a way to limit widgets depending on the field configuration?

swentel’s picture

Hmmm we used to have behaviors in D7 hook_field_widget_info that got removed in D8, not entirely sure if this has anything todo with this

Related issues:

#2047997: Get rid of field_behaviors_widget()
#2056405: Let field types provide their support for "default values" and associated input UI

Note - I hardly think this qualifies as a critical bug though .. :)

berdir’s picture

Yeah, not critical IMHO and was discussed in the list_boolean/boolean merge issue too.

xjm’s picture

Priority: Critical » Major

Agreed that it is not critical. It is goofy though.

berdir’s picture

Assigned: Unassigned » yched

But do we have a way to limit widgets depending on the field configuration?

We do now.

Assigning to @yched to get his attention, we've been discussing/talking about this multiple times.

Anonymous’s picture

I just tested this manually and it seems like the behaviour of the boolean field has changed. Or at least the steps to reproduce this are no longer valid. I'm able to add a boolean field with a cardinality of 3, but I only get a single checkbox when editing a node.

Should we rescope this issue to get rid cardinality setting (and since it would be empty the settings page as a whole) for the boolean field?

Anonymous’s picture

yched’s picture

So, right, boolean widgets and checkboxes have long been a pain with multiple values.

@Damien Tournoud #4 is still true IMO

The field itself with a cardinality > 1 does make sense. It's the checkbox widget that doesn't.

I think we should rather work towards having dedicated widgets for the boolean field type that work OK with multiple fields.

- The decicated BooleanCheckboxWidget currently explicitly only works on the 1st value - checkboxes are painful for multiple values, because they have no "3rd state" for "neither yes or no, just no value". So just displaying it means a value will get saved on submit.

- The "generic" OptionsButtonsWidget works on list_* field types and also on booleans, but similarly isn't a great fit for booleans. Also, boolean field type is no longer part of the list_* family. IMO we should remove booleans from that widget and have a new, dedicated BooleanRadioWidget, which displays "yes / no" radios (which you can leave unchecked for "no value") for each delta.

--> BooleanCheckboxWidget would still be the usual widget for mono-valued bool fields,
and BooleanRadioWidget is the one for multiple-valued widgets ?

We now have WidgetInterface::isApplicable(), that can be used so that BooleanCheckboxWidget is not available for multi-valued fields ?

yched’s picture

I actually started some work on #9 a while ago in a branch somewhere.
Didn't finish, but I could try to un-stale it for now...

aditya.ghan’s picture

I just tested this issue, I'm able to add a boolean field with the cardinality of 3 (or making the field with unlimited values), but still I'm getting only one checkbox when adding/editing the node. I believe this is still a issue that needs to be fixed. I tested on 8.0.x HEAD. Personally I believe we should not have cardinality setting to a boolean field.

aditya.ghan’s picture

Issue tags: +drupalconasia2016
aditya.ghan’s picture

Issue tags: +Triaged for D8 major current state
xjm’s picture

(Updating issue credit for the major triage.)

effulgentsia’s picture

Title: BooleanItem fields and cardinality > 1 does not make sense » "Single on/off checkbox" widget makes no sense for boolean fields with cardinality > 1 and "Check boxes/radio buttons" makes little sense for boolean fields with cardinality > 2
Issue summary: View changes
Issue tags: -Needs issue summary update

I updated the IS with more detail based on what's been said in the comments of this issue, and from my own research.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

xjm’s picture

Issue tags: +Needs usability review, +D8 major triage deferred, +Needs screenshots

Thanks @aditya.ghan for helping triage this.

The core committers discussed this issue today after several of the entity and field API maintainers had a chance to look at it. In terms of the field system, this would just be a normal bug (and the behavior has like this since CCK days so it is nothing new). However, this is primarily a usability problem. @alexpott even said that when he first started using Drupal, this bug caused him a lot of difficulty and confusion.

So, we'd like a usability maintainer's input on whether this is a major usability bug. Deferring triage on that. The summary is a little dense, so can we help the usability review by adding annotated screenshots showing the problem?

yoroy’s picture

I looked up cardinality but still not sure what it means here. Number of allowed values?

xjm’s picture

@yoroy, yep, exactly.

yoroy’s picture

Version: 8.1.x-dev » 8.2.x-dev
Issue summary: View changes
Issue tags: -Needs usability review

Yes, I think this is major. I would like to have an example of where boolean field with multiple allowed values makes sense. My understanding of boolean is that it's always and only a yes/no, 0/1. How would multiple values work? yes/no/maybe or multiple sets of yes/no? I added "Define scenarios/use cases" to the remaining tasks in the issue summary.

No time for a deep dive at time of posting, but yes, lets keep this major please.

skaught’s picture

Would it be best to block bool fields from having cardinality to begin with? It does seem odd to ask the question 'choose yes or no' more than once in in almost any form context.

#20 yes, not, maybe: of course the field should be a select or radioS.
I do know that often enough, a request to build a form comes from 'a designer/the design team' who may not truly understand html form elements, then a dev needs to build a thing to match the design..

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

OWast’s picture

Just adding a note that I built a form with multiple booleans on a D7 this week. Only to discover that it doesn't work. :-)

This is a e-learning/e-health scenario. I have an entity with a two-step form, where the content of step two is dynamically generated using data from step 1. Labels and other info is sourced from step 1. What I need in the submitted form from step 2 is a series of booleans. (ie. True, False, True). I use form_alter to modify the boolean field to fit my purposes. However, it's all in vain as no false values are stored, in the series becomes true, true.

The case is not exactly mainstream, and there are plenty of other ways to solve the problem. If cardinality was hardcoded as 1 on booleans I wouldn't have tried this solution. So that would be better than current situation.

But at heart I feel that developers shouldn't make unnecessary assumptions, and just make the system work logically. And in this case that is to make single checkboxes with multiple cardinality appear as a pile of checkboxes with the exakt same label, potentially with remove and add_more buttons.

hgoto’s picture

I cannot find any real example where boolean field with multiple allowed values makes sense... Even if there's a case where multiple allowed values are needed, other types of list field should be used rather than boolean, I believe. So +1 for "to block bool fields from having cardinality".

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

pameeela’s picture

Added screenshots of current state to IS.

pameeela’s picture

Adding #2619328: A required boolean field behaves differently depending on the widget as related, that issue has much more traction so suggested that we merge them and then can close this as a duplicate.

pameeela’s picture

Issue tags: +Bug Smash Initiative
larowlan’s picture

Status: Active » Closed (duplicate)

Reviewed this in light of #34 and agree this can be closed as a duplicate for the following reasons:

  • both issues are major
  • both are addressing the same problem space
  • the linked issue has a patch
  • the linked issue has input from entity-field maintainers

thanks @pameeela, credited yourself and @aditya.ghan over there

If anyone disagrees with this assessment, please re-open, but I note one month has passed since ##3 so I take that to be there are no objections.

shashank5563’s picture