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,
);
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

chx’s picture

Status: Needs review » Needs work

if attrbitues is not set you get a warning.

use $form['#attributes']['disabled'] = 'disabled'

webchick’s picture

Status: Needs work » Needs review
FileSize
834 bytes

D'oh. Good call. Thanks, chx.

kkaefer’s picture

FileSize
4.35 KB

+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.

Dries’s picture

Status: Needs review » Fixed

Committed to CVS HEAD. Thanks.

webchick’s picture

Thanks a lot!

This has now been documented in both the "updating your modules" section and the Forms API reference.

Anonymous’s picture

Status: Fixed » Closed (fixed)