You are here

function fieldgroup_form_alter in Content Construction Kit (CCK) 6.3

Same name and namespace in other branches
  1. 5 fieldgroup.module \fieldgroup_form_alter()
  2. 6 modules/fieldgroup/fieldgroup.module \fieldgroup_form_alter()
  3. 6.2 modules/fieldgroup/fieldgroup.module \fieldgroup_form_alter()

Implementation of hook_form_alter()

File

modules/fieldgroup/fieldgroup.module, line 455
Create field groups for CCK fields.

Code

function fieldgroup_form_alter(&$form, $form_state, $form_id) {
  if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] . '_node_form' == $form_id) {
    $group_rows = array();
    $field_rows = array();

    //prepare data that will make this easier
    $groups = fieldgroup_groups($form['type']['#value']);
    if (!empty($groups)) {
      foreach ($groups as $name => $more) {
        $group_rows[] = $name;
      }
    }
    $fields = $form['#field_info'];
    if (!empty($fields)) {
      foreach ($fields as $name => $more) {
        $field_rows[] = $name;
      }
    }
    $max_depth = fieldgroup_order_fields_groups($group_rows, $groups, $field_rows);

    //cover the top level fields that aren't in fieldgroups
    if (isset($field_rows)) {
      foreach ($field_rows as $name) {
        $form[$name]['#depth'] = 0;
      }
    }

    //now that we have the order of things as we want them, let's create the fieldsets for the fieldgroups
    foreach ($groups as $group_name => $group) {
      _fieldgroup_add_group_to_form($form, $form_state, $form_id, $group_name, $group, $groups);
    }

    //reorder the groups from the inside-out in order to avoid a recursive function
    while ($max_depth >= 0) {
      foreach ($group_rows as $name) {
        if ($form[$name]['#depth'] == $max_depth) {
          $parent = $form[$name]['#group_parent'];
          if (isset($parent) && $parent != '') {
            $form[$parent][$name] = $form[$name];
            if ($form[$name]['#access']) {
              $form[$parent]['#access'] = TRUE;
            }
            unset($form[$name]);
            $index = array_search($name, $group_rows);
            unset($group_rows[$index]);
          }
        }
      }
      $max_depth--;
    }
  }
  elseif ($form_id == 'content_field_edit_form' && isset($form['widget'])) {
    $content_type = content_types($form['type_name']['#value']);
    $form['widget']['group'] = array(
      '#type' => 'value',
      '#value' => _fieldgroup_field_get_group($content_type['type'], $form['field_name']['#value']),
    );
  }
  elseif ($form_id == 'content_field_overview_form') {
    $form['#validate'][] = 'fieldgroup_field_overview_form_validate';
    $form['#submit'][] = 'fieldgroup_field_overview_form_submit';
  }
  elseif ($form_id == 'content_display_overview_form' && !empty($form['#groups'])) {
    $form['#submit'][] = 'fieldgroup_display_overview_form_submit';
    if (!isset($form['submit'])) {
      $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Save'),
        '#weight' => 10,
      );
    }
  }
  elseif ($form_id == 'content_field_remove_form') {
    $form['#submit'][] = 'fieldgroup_field_remove_form_submit';
  }
}