CCK form field changes using Drupal's hook_form_alter don't work? Use #after_build

Submitted by Charles on Sun, 08/22/2010 - 15:17

I ran into issues when trying to use drupal's hook_form_alter to change some attributes of cck fields in a node form. It appears that depending on the weight of the module the form_alter is in, it might get called before the actual form element has been processed.

To make sure, you can set a function to be called after the form has been properly created, but before rendering.

In your form alter, add the after build key to the form field's array:

$form['field_course_status']['#after_build'][] = 'course_disable_status_field';

Then in your custom function make your changes. You should do a print_r($form_element) or the devel module's dpm($form_element) function to see what the element looks like - I had to put my change into the $form_element['value'] array and not the $form_element itself.

 * Make certain fields in the course form read only
function course_disable_status_field($form_element, &$form_state) {
  $form_element['value']['#attributes'] = array('disabled' => 'disabled');
  return $form_element;

 For reference see: