You are here

function content_multiple_value_form in Content Construction Kit (CCK) 6.2

Same name and namespace in other branches
  1. 6.3 includes/content.node_form.inc \content_multiple_value_form()
  2. 6 includes/content.node_form.inc \content_multiple_value_form()

Special handling to create form elements for multiple values.

Handles generic features for multiple fields:

  • number of widgets
  • AHAH-'add more' button
  • drag-n-drop value reordering
1 call to content_multiple_value_form()
content_field_form in includes/content.node_form.inc
Create a separate form element for each field.

File

includes/content.node_form.inc, line 149
Create fields' form for a content type.

Code

function content_multiple_value_form(&$form, &$form_state, $field, $items) {
  $field_name = $field['field_name'];
  switch ($field['multiple']) {
    case 0:
      $max = 0;
      break;
    case 1:
      $filled_items = content_set_empty($field, $items);
      $current_item_count = isset($form_state['item_count'][$field_name]) ? $form_state['item_count'][$field_name] : count($items);

      // We always want at least one empty icon for the user to fill in.
      $max = $current_item_count > count($filled_items) ? $current_item_count - 1 : $current_item_count;
      break;
    default:
      $max = $field['multiple'] - 1;
      break;
  }
  $title = check_plain(t($field['widget']['label']));
  $description = content_filter_xss(t($field['widget']['description']));
  $form_element = array(
    '#theme' => 'content_multiple_values',
    '#title' => $title,
    '#required' => $field['required'],
    '#description' => $description,
  );
  $function = $field['widget']['module'] . '_widget';
  for ($delta = 0; $delta <= $max; $delta++) {
    if ($element = $function($form, $form_state, $field, $items, $delta)) {
      $defaults = array(
        '#title' => $field['multiple'] >= 1 ? '' : $title,
        '#description' => $field['multiple'] >= 1 ? '' : $description,
        '#required' => $delta == 0 && $field['required'],
        '#weight' => $delta,
        '#delta' => $delta,
        '#columns' => array_keys($field['columns']),
        '#field_name' => $field_name,
        '#type_name' => $field['type_name'],
      );

      // Add an input field for the delta (drag-n-drop reordering), which will
      // be hidden by tabledrag js behavior.
      if ($field['multiple'] >= 1) {

        // We name the element '_weight' to avoid clashing with column names
        // defined by field modules.
        $element['_weight'] = array(
          '#type' => 'weight',
          '#delta' => $max,
          // this 'delta' is the 'weight' element's property
          '#default_value' => isset($items[$delta]['_weight']) ? $items[$delta]['_weight'] : $delta,
          '#weight' => 100,
        );
      }
      $form_element[$delta] = array_merge($element, $defaults);
    }
  }

  // Add AHAH add more button, if not working with a programmed form.
  if ($field['multiple'] == 1 && empty($form['#programmed'])) {

    // Make sure the form is cached so ahah can work.
    $form['#cache'] = TRUE;
    $content_type = content_types($field['type_name']);
    $field_name_css = str_replace('_', '-', $field_name);
    $form_element[$field_name . '_add_more'] = array(
      '#type' => 'submit',
      '#name' => $field_name . '_add_more',
      '#value' => t('Add another item'),
      '#weight' => $field['widget']['weight'] + $max + 1,
      // Submit callback for disabled JavaScript. drupal_get_form() might get
      // the form from the cache, so we can't rely on content_form_alter()
      // including this file. Therefore, call a proxy function to do this.
      '#submit' => array(
        'content_add_more_submit_proxy',
      ),
      '#ahah' => array(
        'path' => 'content/js_add_more/' . $content_type['url_str'] . '/' . $field_name,
        'wrapper' => $field_name_css . '-items',
        'method' => 'replace',
        'effect' => 'fade',
      ),
      // When JS is disabled, the content_add_more_submit handler will find
      // the relevant field using these entries.
      '#field_name' => $field_name,
      '#type_name' => $field['type_name'],
    );

    // Add wrappers for the fields and 'more' button.
    $form_element['#prefix'] = '<div id="' . $field_name_css . '-items">';
    $form_element['#suffix'] = '</div>';
    $form_element[$field_name . '_add_more']['#prefix'] = '<div class="content-add-more clear-block">';
    $form_element[$field_name . '_add_more']['#suffix'] = '</div>';
  }
  return $form_element;
}