Error message

The spam filter installed on this site is currently unavailable. Per site policy, we are unable to accept new submissions until that problem is resolved. Please try resubmitting the form in a couple of minutes.

Modify CCK Fields with Drupal hook_form_alter

Category: 

Here is how to modify a CCK field on a node form in Drupal. In this particular example, the CCK field I am trying to modify is called 'field_news_image' and is inside a CCK Field Group called 'group_image'. The modification hides the field by wrapping a div around the field and setting the div's display to none.

function tf_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case "my_form_id":
      // attach our #after_build handler so we can modify cck fields
      $form['#after_build'][] = 'tf_form_after_build';
      break;
  }
}

function tf_form_after_build ($form, &$form_state) {	
  $form['group_image']['field_news_image']['#prefix'] = "<div style='display: none;'>";
  $form['group_image']['field_news_image']['#suffix'] = "</div>";
  return $form;
}

Here is how to modify a CCK file field's description, this has to be in hook_form_after_build:

$form['field_my_cck_filefield'][0]['#description'] .= "My extra description for the field...";

Add new comment