You are here

function content_field_overview_form in Content Construction Kit (CCK) 6.2

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

Menu callback; listing of fields for a content type.

Allows fields to be reordered and nested in fieldgroups using JS drag-n-drop. Non-CCK form elements can also be moved around.

2 string references to 'content_field_overview_form'
content_menu in ./content.module
Implementation of hook_menu().
fieldgroup_form_alter in modules/fieldgroup/fieldgroup.module
Implementation of hook_form_alter()

File

includes/content.admin.inc, line 129
Administrative interface for content type creation.

Code

function content_field_overview_form(&$form_state, $type_name) {
  content_inactive_message($type_name);

  // When displaying the form, make sure the list of fields
  // is up-to-date.
  if (empty($form_state['post'])) {
    content_clear_type_cache();
  }

  // Gather type information.
  $type = content_types($type_name);
  $fields = $type['fields'];
  $field_types = _content_field_types();
  $extra = $type['extra'];
  $groups = $group_options = $group_types = array();
  if (module_exists('fieldgroup')) {
    $groups = fieldgroup_groups($type['type']);
    $group_types = fieldgroup_types();
    $group_options = _fieldgroup_groups_label($type['type']);

    // Add the ability to group under the newly created row.
    $group_options['_add_new_group'] = '_add_new_group';
  }

  // Store the default weights as we meet them, to be able to put the

  //'add new' rows after them.
  $weights = array();
  $form = array(
    '#tree' => TRUE,
    '#type_name' => $type['type'],
    '#fields' => array_keys($fields),
    '#groups' => array_keys($groups),
    '#extra' => array_keys($extra),
    '#field_rows' => array(),
    '#group_rows' => array(),
  );

  // Fields.
  foreach ($fields as $name => $field) {
    $weight = $field['widget']['weight'];
    $form[$name] = array(
      'label' => array(
        '#value' => check_plain($field['widget']['label']),
      ),
      'field_name' => array(
        '#value' => $field['field_name'],
      ),
      'type' => array(
        '#value' => t($field_types[$field['type']]['label']),
      ),
      'configure' => array(
        '#value' => l(t('Configure'), 'admin/content/node-type/' . $type['url_str'] . '/fields/' . $field['field_name']),
      ),
      'remove' => array(
        '#value' => l(t('Remove'), 'admin/content/node-type/' . $type['url_str'] . '/fields/' . $field['field_name'] . '/remove'),
      ),
      'weight' => array(
        '#type' => 'textfield',
        '#default_value' => $weight,
        '#size' => 3,
      ),
      'parent' => array(
        '#type' => 'select',
        '#options' => $group_options,
        '#default_value' => '',
      ),
      'prev_parent' => array(
        '#type' => 'hidden',
        '#value' => '',
      ),
      'hidden_name' => array(
        '#type' => 'hidden',
        '#default_value' => $field['field_name'],
      ),
      '#leaf' => TRUE,
      '#row_type' => 'field',
      'field' => array(
        '#type' => 'value',
        '#value' => $field,
      ),
    );
    if ($field['locked']) {
      $form[$name]['configure'] = array(
        '#value' => t('Locked'),
      );
      $form[$name]['remove'] = array();
      $form[$name]['#disabled_row'] = TRUE;
    }
    $form['#field_rows'][] = $name;
    $weights[] = $weight;
  }

  // Groups.
  foreach ($groups as $name => $group) {
    $weight = $group['weight'];
    $form[$name] = array(
      'label' => array(
        '#value' => check_plain($group['label']),
      ),
      'group_name' => array(
        '#value' => $group['group_name'],
      ),
      'group_type' => array(
        '#value' => t($group_types[$group['group_type']]),
      ),
      'configure' => array(
        '#value' => l(t('Configure'), 'admin/content/node-type/' . $type['url_str'] . '/groups/' . $group['group_name']),
      ),
      'remove' => array(
        '#value' => l(t('Remove'), 'admin/content/node-type/' . $type['url_str'] . '/groups/' . $group['group_name'] . '/remove'),
      ),
      'weight' => array(
        '#type' => 'textfield',
        '#default_value' => $weight,
        '#size' => 3,
      ),
      'parent' => array(
        '#type' => 'hidden',
        '#default_value' => '',
      ),
      'hidden_name' => array(
        '#type' => 'hidden',
        '#default_value' => $group['group_name'],
      ),
      '#root' => TRUE,
      '#row_type' => 'group',
      'group' => array(
        '#type' => 'value',
        '#value' => $group,
      ),
    );

    // Adjust child fields rows.
    foreach ($group['fields'] as $field_name => $field) {
      $form[$field_name]['parent']['#default_value'] = $name;
      $form[$field_name]['prev_parent']['#value'] = $name;
    }
    $form['#group_rows'][] = $name;
    $weights[] = $weight;
  }

  // Non-CCK 'fields'.
  foreach ($extra as $name => $label) {
    $weight = $extra[$name]['weight'];
    $form[$name] = array(
      'label' => array(
        '#value' => check_plain(t($extra[$name]['label'])),
      ),
      'description' => array(
        '#value' => isset($extra[$name]['description']) ? $extra[$name]['description'] : '',
      ),
      'weight' => array(
        '#type' => 'textfield',
        '#default_value' => $weight,
        '#size' => 3,
      ),
      'parent' => array(
        '#type' => 'hidden',
        '#default_value' => '',
      ),
      'configure' => array(
        '#value' => isset($extra[$name]['configure']) ? $extra[$name]['configure'] : '',
      ),
      'remove' => array(
        '#value' => isset($extra[$name]['remove']) ? $extra[$name]['remove'] : '',
      ),
      'hidden_name' => array(
        '#type' => 'hidden',
        '#default_value' => $name,
      ),
      '#leaf' => TRUE,
      '#root' => TRUE,
      '#disabled_row' => TRUE,
      '#row_type' => 'extra',
    );
    $form['#field_rows'][] = $name;
    $weights[] = $weight;
  }

  // Additional row : add new field.
  $weight = max($weights) + 1;
  $field_type_options = content_field_type_options();
  $widget_type_options = content_widget_type_options(NULL, TRUE);
  if ($field_type_options && $widget_type_options) {
    array_unshift($field_type_options, t('- Select a field type -'));
    array_unshift($widget_type_options, t('- Select a widget -'));
    $name = '_add_new_field';
    $form[$name] = array(
      'label' => array(
        '#type' => 'textfield',
        '#size' => 15,
        '#description' => t('Label'),
      ),
      'field_name' => array(
        '#type' => 'textfield',
        // This field should stay LTR even for RTL languages.
        '#field_prefix' => '<span dir="ltr">field_',
        '#field_suffix' => '</span>&lrm;',
        '#attributes' => array(
          'dir' => 'ltr',
        ),
        '#size' => 15,
        // Field names are limited to 32 characters including the 'field_'
        // prefix which is 6 characters long.
        '#maxlength' => 26,
        '#description' => t('Field name (a-z, 0-9, _)'),
      ),
      'type' => array(
        '#type' => 'select',
        '#options' => $field_type_options,
        '#description' => theme('advanced_help_topic', 'content', 'fields') . t('Type of data to store.'),
      ),
      'widget_type' => array(
        '#type' => 'select',
        '#options' => $widget_type_options,
        '#description' => t('Form element to edit the data.'),
      ),
      'weight' => array(
        '#type' => 'textfield',
        '#default_value' => $weight,
        '#size' => 3,
      ),
      'parent' => array(
        '#type' => 'select',
        '#options' => $group_options,
        '#default_value' => '',
      ),
      'hidden_name' => array(
        '#type' => 'hidden',
        '#default_value' => $name,
      ),
      '#leaf' => TRUE,
      '#add_new' => TRUE,
      '#row_type' => 'add_new_field',
    );
    $form['#field_rows'][] = $name;
  }

  // Additional row : add existing field.
  $existing_field_options = content_existing_field_options($type_name);
  if ($existing_field_options && $widget_type_options) {
    $weight++;
    array_unshift($existing_field_options, t('- Select an existing field -'));
    $name = '_add_existing_field';
    $form[$name] = array(
      'label' => array(
        '#type' => 'textfield',
        '#size' => 15,
        '#description' => t('Label'),
      ),
      'field_name' => array(
        '#type' => 'select',
        '#options' => $existing_field_options,
        '#description' => t('Field to share'),
      ),
      'widget_type' => array(
        '#type' => 'select',
        '#options' => $widget_type_options,
        '#description' => t('Form element to edit the data.'),
      ),
      'weight' => array(
        '#type' => 'textfield',
        '#default_value' => $weight,
        '#size' => 3,
      ),
      'parent' => array(
        '#type' => 'select',
        '#options' => $group_options,
        '#default_value' => '',
      ),
      'hidden_name' => array(
        '#type' => 'hidden',
        '#default_value' => $name,
      ),
      '#leaf' => TRUE,
      '#add_new' => TRUE,
      '#row_type' => 'add_existing_field',
    );
    $form['#field_rows'][] = $name;
  }

  // Additional row : add new group.
  if (!empty($group_types)) {
    $weight++;
    $name = '_add_new_group';
    $form[$name] = array(
      'label' => array(
        '#type' => 'textfield',
        '#size' => 15,
        '#description' => t('Label'),
      ),
      'group_name' => array(
        '#type' => 'textfield',
        // This field should stay LTR even for RTL languages.
        '#field_prefix' => '<span dir="ltr">group_',
        '#field_suffix' => '</span>&lrm;',
        '#attributes' => array(
          'dir' => 'ltr',
        ),
        '#size' => 15,
        // Group names are limited to 32 characters including the 'group_'
        // prefix which is 6 characters long.
        '#maxlength' => 26,
        '#description' => t('Group name (a-z, 0-9, _)'),
      ),
      'group_option' => array(
        '#type' => 'hidden',
        '#value' => '',
      ),
      'group_type' => array(
        '#type' => 'hidden',
        '#value' => 'standard',
      ),
      'weight' => array(
        '#type' => 'textfield',
        '#default_value' => $weight,
        '#size' => 3,
      ),
      'parent' => array(
        '#type' => 'hidden',
        '#default_value' => '',
      ),
      'hidden_name' => array(
        '#type' => 'hidden',
        '#default_value' => $name,
      ),
      '#root' => TRUE,
      '#add_new' => TRUE,
      '#row_type' => 'add_new_group',
    );
    if (count($group_types) > 1) {
      $form[$name]['group_type'] = array(
        '#type' => 'select',
        '#description' => t('Type of group.'),
        '#options' => $group_types,
        '#default_value' => 'standard',
      );
    }
    $form['#group_rows'][] = $name;
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  return $form;
}