You are here

function content_admin_field_overview_form in Content Construction Kit (CCK) 5

Same name and namespace in other branches
  1. 6 includes/content.admin.inc \content_admin_field_overview_form()

Menu callback; presents a listing of fields for a content type.

Form includes form widgets to set weight and group for each item and displays other form elements and their weights to make it easier to place CCK fields in the form and see where they will appear.

1 string reference to 'content_admin_field_overview_form'
content_menu in ./content.module
Implementation of hook_menu().

File

./content_admin.inc, line 44
Administrative interface for content type creation.

Code

function content_admin_field_overview_form($type_name) {
  $form = array();
  $type = content_types($type_name);
  $field_types = _content_field_types();

  // Create a dummy node and form and call hook_form_alter()
  // to produce an array of fields and weights added to the node by all modules.
  $dummy_node = new stdClass();
  $dummy_node->type = $type['type'];

  // Some modules (userreview...) "hide" their node forms, resulting in no field
  // being listed. We set a special flag to inform them this form is special.
  $dummy_node->cck_dummy_node_form = TRUE;
  $dummy_form_id = $type['type'] . '_node_form';
  $dummy_form = node_form($dummy_node);
  foreach (module_implements('form_alter') as $module) {
    $function = $module . '_form_alter';
    $function($dummy_form_id, $dummy_form);
  }

  // Move group fields into a 'fields' subgroup to make them easier to identify.
  // Remove fields that are used in groups from the form, the group will handle them.
  if (module_exists('fieldgroup')) {
    $form['#groups'] = fieldgroup_groups($type['type']);
    $form['#group_labels'] = _fieldgroup_groups_label($type['type']);
    if (!$form['#groups']) {
      drupal_set_message(t('There are no groups configured for this content type.'));
    }
    foreach ($form['#groups'] as $group) {
      foreach ($group['fields'] as $field_name => $field) {
        unset($dummy_form[$field_name]);
      }
    }
  }
  if (!$type['fields']) {
    drupal_set_message(t('There are no fields configured for this content type.'));
  }
  if (!$type['fields'] && !$form['#groups']) {
    return $form;
  }
  $form['disabled']['#value'] = array();

  // Iterate through the dummy form and add top-level fields and weights to a table.
  // Construct the table values in an array '#table' that FAPI will ignore, keyed on the item's weight.
  // Create separate form elements for each weight and group value and put a placeholder for each in #table.
  foreach ($dummy_form as $key => $value) {

    // Limiting weight to < 10 will keep workflow and submit elements from being added to the overview table.
    // They're outside the weight range allowed for CCK fields, so won't interfere with field placement.
    if (is_array($value) && (isset($value['#weight']) || $key == 'body_filter') && $value['#weight'] <= 10) {

      // if this item is a group, insert group info into table, then add all the group fields below it
      if (substr($key, 0, 6) == 'group_' && isset($form['#groups'])) {
        $row = $group_form = array();
        $row['label'] = $form['#group_labels'][$form['#groups'][$key]['group_name']];
        $row['name'] = $form['#groups'][$key]['group_name'];
        $row['type'] = t('group');
        $row['weights'] = 'form-group-weights';
        $row['groups'] = '';
        $row['configure'] = l(t('configure'), 'admin/content/types/' . $type['url_str'] . '/groups/' . $form['#groups'][$key]['group_name'] . '/edit');
        $row['remove'] = l(t('remove'), 'admin/content/types/' . $type['url_str'] . '/groups/' . $form['#groups'][$key]['group_name'] . '/remove');
        $data = $row;
        $form['group-weights'][$key] = array(
          '#type' => 'weight',
          '#default_value' => $value['#weight'],
        );
        foreach ($form['#groups'][$key]['fields'] as $field_name => $field) {
          $row = array();
          $field = $type['fields'][$field_name];
          $row['label'] = check_plain($field['widget']['label']);
          $row['name'] = $field['field_name'];
          $row['type'] = $field_types[$field['type']]['label'];
          $row['weights'] = 'form-field-weights';
          $row['groups'] = 'form-field-groups';
          $row['configure'] = l(t('configure'), 'admin/content/types/' . $type['url_str'] . '/fields/' . $field_name);
          $row['remove'] = l(t('remove'), 'admin/content/types/' . $type['url_str'] . '/fields/' . $field_name . '/remove');
          $group_form[$field['widget']['weight']][] = array(
            $field_name => $row,
          );
          $form['field-weights'][$field_name] = array(
            '#type' => 'weight',
            '#default_value' => $field['widget']['weight'],
          );
          $form['field-groups'][$field_name] = array(
            '#type' => 'select',
            '#options' => $form['#group_labels'],
            '#default_value' => fieldgroup_get_group($type['type'], $field_name),
          );
          $form['field-groups-defaults'][$field_name] = array(
            '#type' => 'hidden',
            '#value' => fieldgroup_get_group($type['type'], $field_name),
          );
        }

        // sort the group fields by weight
        ksort($group_form);
        $group = (array) $data + array(
          'fields' => $group_form,
        );
        $form['#table'][$value['#weight']][] = array(
          $key => $group,
        );
      }
      elseif (substr($key, 0, 6) == 'field_') {
        $row = array();
        $field = $type['fields'][$key];
        $row['label'] = check_plain($field['widget']['label']);
        $row['name'] = $field['field_name'];
        $row['type'] = $field_types[$field['type']]['label'];
        $row['weights'] = 'form-field-weights';
        if (isset($form['#groups'])) {
          $row['groups'] = 'form-field-groups';
        }
        $row['configure'] = l(t('configure'), 'admin/content/types/' . $type['url_str'] . '/fields/' . $key);
        $row['remove'] = l(t('remove'), 'admin/content/types/' . $type['url_str'] . '/fields/' . $key . '/remove');
        $form['#table'][$field['widget']['weight']][] = array(
          $key => $row,
        );
        $form['field-weights'][$key] = array(
          '#type' => 'weight',
          '#default_value' => $field['widget']['weight'],
        );
        if (isset($form['#groups'])) {
          $form['field-groups'][$key] = array(
            '#type' => 'select',
            '#options' => $form['#group_labels'],
            '#default_value' => fieldgroup_get_group($type['type'], $key),
          );
        }
      }
      else {
        $row = array();
        $row['label'] = $key == 'body_filter' ? t('body') : $key;
        $row['name'] = $key;
        $row['type'] = $key;
        $row['weights'] = 'form-field-weights';
        if (isset($form['#groups'])) {
          $row['groups'] = '';
        }
        $row['configure'] = '';
        $row['remove'] = '';
        $form['#table'][$value['#weight']][] = array(
          $key => $row,
        );
        $form['disabled']['#value'][] = $key;
        $form['field-weights'][$key] = array(
          '#type' => 'weight',
          '#default_value' => $value['#weight'],
          '#disabled' => TRUE,
        );
      }
    }
  }

  // sort the table by weight
  ksort($form['#table']);

  // add submit buttons and hidden fields
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Update'),
  );
  $form['field-weights']['#tree'] = TRUE;
  $form['group-weights']['#tree'] = TRUE;
  $form['field-groups']['#tree'] = TRUE;
  $form['field-groups-defaults']['#tree'] = TRUE;
  $form['disabled']['#type'] = 'hidden';
  $form['disabled']['#value'] = serialize($form['disabled']['#value']);
  $form['type_name']['#type'] = 'hidden';
  $form['type_name']['#value'] = $type['type'];
  return $form;
}