You are here

function content_multiple_value_form in Content Construction Kit (CCK) 6

Same name and namespace in other branches
  1. 6.3 includes/content.node_form.inc \content_multiple_value_form()
  2. 6.2 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 128

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:
      $max = isset($form_state['item_count'][$field_name]) ? $form_state['item_count'][$field_name] : count($items);
      break;
    default:
      $max = $field['multiple'] - 1;
      break;
  }
  $form_element = array(
    // TODO : Not sure about that one - when theming through '#type',
    // children are already rendered, and we can't do the table layout.

    //'#type' => 'content_multiple_values',
    '#theme' => 'content_multiple_values',
    '#title' => t($field['widget']['label']),
    '#required' => $field['required'],
    '#description' => t($field['widget']['description']),
  );
  $function = $field['module'] . '_widget';
  for ($delta = 0; $delta <= $max; $delta++) {
    if ($element = $function($form, $form_state, $field, $items, $delta)) {
      $defaults = array(
        '#field_name' => $field_name,
        '#title' => $field['multiple'] >= 1 ? '' : t($field['widget']['label']),
        '#description' => $field['multiple'] >= 1 ? '' : t($field['widget']['description']),
        '#required' => $delta == 0 && $field['required'],
        '#weight' => $delta,
        '#delta' => $delta,
        '#columns' => array_keys($field['columns']),
      );

      // 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(
          // When JS is disabled, a weight selector will be more convenient (?),
          // but won't work well with more than 10 values.
          '#type' => $max < 10 ? 'weight' : 'textfield',
          '#default_value' => $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($form['type']['#value']);
    $field_name_css = str_replace('_', '-', $field_name);
    $form_element[$field_name . '_add_more'] = array(
      '#type' => 'submit',
      '#value' => t('Add another !field value', array(
        '!field' => t($field['widget']['label']),
      )),
      '#description' => t("If the amount of boxes above isn't enough, click here to add more items."),
      '#weight' => $field['widget']['weight'] + $max + 1,
      '#submit' => array(
        'content_add_more_submit',
      ),
      // If no JavaScript action.
      '#ahah' => array(
        'path' => 'content/js_add_more/' . $content_type['url_str'] . '/' . $field_name,
        'wrapper' => $field_name_css . '-items',
        'method' => 'replace',
        'effect' => 'fade',
      ),
    );

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