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.
At least once per week (sometimes more often), I answer the question in IRC:
"How do I disable a textfield/checkbox/etc.?"
The answer currently is to do:
$form['#attributes'] = array('disabled' => 'disabled');
However, this is not intuitive; when most people think of #attributes, they think of "extra" things like "class" and stuff... not attributes that are innately part of input, button, etc. elements.
So this patch adds a #disabled property so that you can simply do:
$form['textfield'] = array(
'#type' => 'textfield',
'#title' => t("Some textfield you can't change"),
'#disabled' => TRUE,
);
Comment | File | Size | Author |
---|---|---|---|
#3 | form_disabled_1.patch | 4.35 KB | kkaefer |
#2 | form_disabled_0.patch | 834 bytes | webchick |
form_disabled.patch | 878 bytes | webchick | |
Comments
Comment #1
chx CreditAttribution: chx commentedif attrbitues is not set you get a warning.
use $form['#attributes']['disabled'] = 'disabled'
Comment #2
webchickD'oh. Good call. Thanks, chx.
Comment #3
kkaefer CreditAttribution: kkaefer commented+1 on this.
I replaced all occurrences of this with
#disabled = FALSE
. I think this can go into this patch, too because that change is not very complicated and/or bloated.Comment #4
Dries CreditAttribution: Dries commentedCommitted to CVS HEAD. Thanks.
Comment #5
webchickThanks a lot!
This has now been documented in both the "updating your modules" section and the Forms API reference.
Comment #6
(not verified) CreditAttribution: commented