Drupal 6: How to set a form field created with CCK to disabled

Drupal 6: How to set a form field created with CCK to disabled

Form fields are very easy to manipulate through a form_alter in a module, however when we are dealing with fields created with CCK (Content Construction Kit) the story is a bit different because these fields are created at the end of the form construction, in such a way that any change that we need to make must be done between the construction of the form and the rendering.

#after_build

In the hook_form_alter we need to define the function to be executed after the form is built using the special element #after_build (http://api.drupal.org/api/drupal/developer--topics--forms_api_reference…)

function mymodule_form_alter(&$form, &$form_state, $form_id){

...

$form['#after_build'][] = 'mymodule_after_build';

...

}

The next step is to create the function we specified and make the change to change by specifying the 'disabled' attribute

function mymodule_after_build($form, &$form_state){

...

$form['field_myfield'][0]['value']['#attributes']['disabled'] = TRUE;

...

return $form;

}

If you have any concern or contribution to make, contact us through this form or our Twitter account @seedcolombia