Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
The attached patch allows to disable one or more items in a group of checkboxes or radios. You just need to enter the #disabled as array and not as boolean value, like this:
$options = array('1' => 'First item', '2' => 'Second item', '3' => 'Third item', '4' => 'Fourth item');
$disabled = array(2, 3);
$form['checkboxes'] = array('#type' => 'checkboxes', '#options' => $options, '#disabled' => $disabled);
In this example the checkboxes with keys 2 and 3 will be disabled. The #disabled array must contain the keys of the items that must be disabled. When #disabled is boolean, it works as usual and all items will be disabled.
Comment | File | Size | Author |
---|---|---|---|
#22 | form-disable-checkbox-104715-22.patch | 1.32 KB | cburschka |
#19 | form.inc_104715_disableCheckboxes_4.patch | 1.2 KB | jeffschuler |
#17 | form.inc_104715_disableCheckboxes_3.patch | 1.19 KB | jeffschuler |
#14 | form.inc_104715_disableCheckboxes.patch | 1.07 KB | jeffschuler |
#10 | form.inc-disabled_10.patch | 1.51 KB | Pancho |
Comments
Comment #1
ximo CreditAttribution: ximo commented+1 for this patch, I think the Forms API should include this
Comment #2
ximo CreditAttribution: ximo commentedCould the Forms API for checkboxes and radios be rewritten to support the following code?
In other words, have Forms API support the #checkbox/#radio type for every checkbox/radio option, giving each option the same properties as a stand-alone checkbox/radio. This would allow for more advanced forms, and would make this suggestion easy to implement. However it looks like a lot of rewrite, and unfortunately I don't know form.inc enough to implement this myself (yet).
Comment #3
ximo CreditAttribution: ximo commentedHere's a an updated version of stafano73's patch for revision 1.177..
Comment #4
shunting CreditAttribution: shunting commentedUntil the forms API does include it, you can set the #attributes property of the individual form element (button, box, etc) to something like array('disabled'=>'disabled') if you want.
See
http://api.drupal.org/api/HEAD/file/developer/topics/forms_api_reference...
Less than elegant but it does work.
Comment #5
tonyn CreditAttribution: tonyn commented+1, looking forward to this.
Comment #6
Junyor CreditAttribution: Junyor commentedIt would be good to be able to disable individual options in a SELECT element, too.
Comment #7
scafmac CreditAttribution: scafmac commentedIs a patch really needed? You can control individual radio buttons with the 5.x forms api - http://drupal.org/node/68740#comment-268897 - you just cannot use the '#options' item...
Comment #8
Junyor CreditAttribution: Junyor commentedYour approach is rather difficult and hidden compared to just having #options. This should apply to radio buttons, check boxes, options, and optgroups.
Comment #9
chx CreditAttribution: chx commentedPatch needs reroll and despite this is a cool feature, we are not adding new features, small or big to core now.
Comment #10
PanchoHere's a rerolled patch against HEAD (which is now 7.x-dev). Please test.
Comment #11
PanchoRemark: This one here is blocking #119038
Comment #12
kenorb CreditAttribution: kenorb commentedFor disabling specified checkbox you can use followed function in form_alter hook:
Or for disabling field this one:
Comment #13
lilou CreditAttribution: lilou commentedPatch no longer applied.
Comment #14
jeffschuler+1 for this functionality.
Re-rolled Pancho's #10 to apply to HEAD.
Comment #15
catchThis looks good - seems like we could use it for the forum select to exclude containers? That'd make it easy to write a test for this as well.
Comment #17
jeffschulerRe-rolled again for current HEAD, and resolved an issue that testbot was choking on.
@catch, I'm not very familiar... If you could please give me more info on use "for the forum select to exclude containers", I'll see what I can do.
Comment #19
jeffschulerMissed the change from
$form
to$element
in_form_builder_handle_input_element()
.Comment #21
catchWould be good if this could be re-rolled with -up to get function names for context.
Also we should apply this to forum containers to remove some horribly validation code.
Comment #22
cburschkaHere's the -up.
Comment #23
TomSherlock CreditAttribution: TomSherlock commentedForgive my ignorance; i'm not familiar with the proper way to construct patches.
Is this a typo:
@@ -2290,6 +2295,11 @@ function form_process_checkboxes($elemen
Should that be element?
Comment #24
effulgentsia CreditAttribution: effulgentsia commentedSeems same / very similar to #284917: Allow FAPI select, radios, and checkboxes to specify some options as disabled, so let's join forces over there. Marking this one as the duplicate rather than that one, because I prefer the use of a new property #disabled_options as is done on that issue rather than overloading #disabled.
Comment #25
jonhattan