function content_multiple_value_form in Content Construction Kit (CCK) 6.3
Same name and namespace in other branches
- 6 includes/content.node_form.inc \content_multiple_value_form()
- 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 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:
$deltas = array(
0,
);
$max = 0;
break;
case 1:
$deltas = array_keys($items);
$current_item_count = max(1, isset($form_state['item_count'][$field_name]) ? $form_state['item_count'][$field_name] : count($deltas));
$max = !empty($deltas) ? max($deltas) : -1;
while (count($deltas) < $current_item_count) {
$max++;
$deltas[] = $max;
}
break;
default:
$max = $field['multiple'] - 1;
$deltas = range(0, $max);
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';
foreach ($deltas as $delta) {
if ($element = $function($form, $form_state, $field, $items, $delta)) {
$defaults = array(
'#title' => $field['multiple'] >= 1 ? '' : $title,
'#description' => $field['multiple'] >= 1 ? '' : $description,
'#required' => $field['multiple'] == 0 ? $field['required'] : FALSE,
'#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,
);
}
// Add a checkbox to allow users remove a single delta item.
// See content_set_empty() and theme_content_multiple_values().
if ($field['multiple'] == 1) {
// We name the element '_remove' to avoid clashing with column names
// defined by field modules.
$element['_remove'] = array(
'#type' => 'checkbox',
'#attributes' => array(
'class' => 'content-multiple-remove-checkbox',
),
'#default_value' => isset($items[$delta]['_remove']) ? $items[$delta]['_remove'] : 0,
);
}
$form_element[$delta] = array_merge($element, $defaults);
}
}
// Add an #after_build callback to prevent validation of fields that are
// flagged for removal and enforce field requirement settings.
if ($field['multiple'] >= 1) {
$form_element['#after_build'] = array(
'content_multiple_value_after_build_proxy',
);
}
// 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;
}