field_multiple_value_form($field, $instance, $langcode, $items, &$form, &$form_state)Special handling to create form elements for multiple values.
Handles generic features for multiple fields:
modules/field/field.form.inc, line 132
<?php
function field_multiple_value_form($field, $instance, $langcode, $items, &$form, &$form_state) {
// This form has its own multistep persistance.
if ($form_state['rebuild']) {
$form_state['input'] = array();
}
$field_name = $field['field_name'];
// Determine the number of widgets to display.
switch ($field['cardinality']) {
case FIELD_CARDINALITY_UNLIMITED:
$filled_items = _field_filter_items($field, $items);
$current_item_count = isset($form_state['field_item_count'][$field_name])
? $form_state['field_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['cardinality'] - 1;
break;
}
$title = check_plain(t($instance['label']));
$description = field_filter_xss(t($instance['description']));
$wrapper_id = drupal_html_class($field_name) . '-add-more-wrapper';
$field_elements = array();
$function = $instance['widget']['module'] . '_field_widget_form';
if (function_exists($function)) {
for ($delta = 0; $delta <= $max; $delta++) {
$multiple = $field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED;
$element = array(
'#object_type' => $instance['object_type'],
'#bundle' => $instance['bundle'],
'#field_name' => $field_name,
'#language' => $langcode,
'#columns' => array_keys($field['columns']),
// For multiple fields, title and description are handled by the wrapping table.
'#title' => $multiple ? '' : $title,
'#description' => $multiple ? '' : $description,
// Only the first widget should be required.
'#required' => $delta == 0 && $instance['required'],
'#delta' => $delta,
'#weight' => $delta,
);
if ($element = $function($form, $form_state, $field, $instance, $langcode, $items, $delta, $element)) {
// Input field for the delta (drag-n-drop reordering).
if ($multiple) {
// We name the element '_weight' to avoid clashing with elements
// defined by widget.
$element['_weight'] = array(
'#type' => 'weight',
// Note: this 'delta' is the FAPI 'weight' element's property.
'#delta' => $max,
'#default_value' => isset($items[$delta]['_weight']) ? $items[$delta]['_weight'] : $delta,
'#weight' => 100,
);
}
$field_elements[$delta] = $element;
}
}
if ($field_elements) {
$field_elements += array(
'#theme' => 'field_multiple_value_form',
'#field_name' => $field['field_name'],
'#cardinality' => $field['cardinality'],
'#title' => $title,
'#required' => $instance['required'],
'#description' => $description,
'#prefix' => '<div id="' . $wrapper_id . '">',
'#suffix' => '</div>',
'#max_delta' => $max,
);
// Add 'add more' button, if not working with a programmed form.
if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED && empty($form_state['programmed'])) {
$field_elements['add_more'] = array(
'#type' => 'submit',
'#name' => $field_name . '_add_more',
'#value' => t('Add another item'),
'#attributes' => array('class' => array('field-add-more-submit')),
'#limit_validation_errors' => array(array($field_name, $langcode)),
'#submit' => array('field_add_more_submit'),
'#ajax' => array(
'callback' => 'field_add_more_js',
'wrapper' => $wrapper_id,
'method' => 'replace',
'effect' => 'fade',
),
// The field_add_more_submit() and field_add_more_js() handlers will
// find the relevant field using those entries.
'#field_name' => $field_name,
'#language' => $langcode,
);
}
}
}
return $field_elements;
}
?>