As of today, grouping is only possible for select lists, not for checkboxes and radio buttons. In form.inc, options buttons get flattened out.
If you read this thread: #1878292: Add grouping by Vocabulary / Content type / Bundle for check boxes / radio buttons widget you can see that grouping for checkboxes and radio buttons is a legitimate feature request for entity reference fields.
I believe this can be accomplished by doing the following:
1/ setting optgroups property to TRUE for options_buttons in function _options_properties() of modules/field/modules/options.module file. This prevents #options from being flattened out.
But the presentation layer does not handle grouping for checkboxes and radios... so:
2/ modify function form_process_checkboxes in includes/form.inc to actually handle grouping. I've only tested for checkboxes so far but I believe the same can be done for radio buttons in form_process_radios.
3/ finally for the system not to throw an illegal choice error at form validation, we actually need to flatten the options before validation process the same way its done for select lists right now in function _form_validate of include/form.inc
In order not to break already existing Drupal sites, I suggest we wrap steps 1 and 3 in conditional statement based on either :
- a variable in settings.php
- or maybe more a Drupal variable (variable_get) configurable in the UI that wouldn't need to be copied manually and maybe forgotten each time we need to update settings.php.
Note that modifications in 2) would not affect current Drupal sites since all the grouping code would only be executed for Arrays() but all options would still be flattened out... so no array, no new behavior.
Could this be added to the Drupal 7 core or is it too late? I will of course provide my code as soon as it is more complete.