function _content_multigroup_fieldgroup_form in Content Construction Kit (CCK) 6.3
Implementation of hook_fieldgroup_form().
Align the delta values of each field in the Multigroup.
Swap the field name and delta for each Multigroup so we can d-n-d each collection of fields as a single delta item.
1 call to _content_multigroup_fieldgroup_form()
- content_multigroup_fieldgroup_form in modules/
content_multigroup/ content_multigroup.module - Implementation of hook_fieldgroup_form().
File
- modules/
content_multigroup/ content_multigroup.node_form.inc, line 16 - Implementation of node edit functions for content multigroup.
Code
function _content_multigroup_fieldgroup_form(&$form, &$form_state, $form_id, $group) {
$node = $form['#node'];
$group_name = $group['group_name'];
$group_multiple = (int) $group['settings']['multigroup']['multiple'];
$content_type = content_types($group['type_name']);
// Build list of accessible fields in this group.
$group_fields = array();
foreach ($content_type['fields'] as $field_name => $field) {
if (isset($group['fields'][$field_name]) && isset($form[$group_name][$field_name])) {
if (!isset($form[$group_name][$field_name]['#access']) || $form[$group_name][$field_name]['#access']) {
$group_fields[$field_name] = $field;
}
}
}
// Quit if there are no field in the form for this group.
if (empty($group_fields)) {
return;
}
switch ($group_multiple) {
case 0:
$group_deltas = array(
0,
);
$max_delta = 0;
break;
case 1:
// Compute unique deltas from all deltas used by fields in this multigroup.
$group_deltas = array();
$max_delta = -1;
foreach (array_keys($group_fields) as $field_name) {
if (!empty($node->{$field_name}) && is_array($node->{$field_name})) {
foreach (array_keys($node->{$field_name}) as $delta) {
$group_deltas[$delta] = $delta;
}
sort($group_deltas);
$max_delta = max($max_delta, max($group_deltas));
}
}
$current_item_count = isset($form_state['item_count'][$group_name]) ? $form_state['item_count'][$group_name] : max(1, count($group_deltas));
while (count($group_deltas) < $current_item_count) {
$max_delta++;
$group_deltas[] = $max_delta;
}
break;
default:
$max_delta = $group_multiple - 1;
$group_deltas = range(0, $max_delta);
break;
}
$form[$group_name]['#theme'] = 'content_multigroup_node_form';
$form[$group_name]['#item_count'] = count($group_deltas);
$form[$group_name]['#type_name'] = $group['type_name'];
$form[$group_name]['#group_name'] = $group_name;
$form[$group_name]['#group_label'] = $group['label'];
$form[$group_name]['#group_fields'] = $group_fields;
$form[$group_name]['#tree'] = TRUE;
// Multigroups cannot be vertical tabs, don't let Vertical Tabs module try to do that.
$form[$group_name]['#group'] = FALSE;
if (!isset($form['#multigroups'])) {
$form['#multigroups'] = array();
}
$form['#multigroups'][$group_name] = $group_fields;
// Add a visual indication to the fieldgroup title if the multigroup is required.
if (!empty($group['settings']['multigroup']['required'])) {
$form[$group_name]['#title'] .= ' <span class="form-required" title="' . t('This group requires one collection of fields minimum.') . '">*</span>';
}
// Attach our own after build handler to the form, used to fix posting data
// and the form structure, moving fields back to their original positions.
// That is, move them from group->delta->field back to field->delta.
if (!isset($form['#after_build'])) {
$form['#after_build'] = array();
}
if (!in_array('content_multigroup_node_form_after_build', $form['#after_build'])) {
array_unshift($form['#after_build'], 'content_multigroup_node_form_after_build');
}
// Attach our own validation handler to the form, used to check for empty fields.
if (!isset($form['#validate'])) {
$form['#validate'] = array();
}
if (!in_array('content_multigroup_node_form_validate', $form['#validate'])) {
array_unshift($form['#validate'], 'content_multigroup_node_form_validate');
}
$elements[$group_name] = array();
foreach ($group_deltas as $delta) {
$element = content_multigroup_group_form($form, $form_state, $group, $delta);
$elements[$group_name] = array_merge($elements[$group_name], $element[$group_name]);
}
$form[$group_name] = $elements[$group_name];
// Unset the original group field values now that we've moved them.
foreach (array_keys($group_fields) as $field_name) {
unset($form[$group_name][$field_name]);
}
// Disable required flag during FormAPI validation, except when building the
// form for an 'Add more values' request, then replace it in pre_render.
// To avoid re-creating this array of values over and over, create it once
// and store it as a form attribute.
$form['#multigroup_required_fields'] = array();
if (empty($form_state['multigroup_add_more'])) {
foreach ($form['#multigroups'] as $group_name => $group_fields) {
$form['#multigroup_required_fields'][$group_name] = array();
foreach ($group_fields as $field_name => $field) {
if ($field['required']) {
$form['#multigroup_required_fields'][$group_name][$field_name] = TRUE;
$form['#field_info'][$field_name]['required'] = FALSE;
}
}
}
if (!isset($form['#pre_render'])) {
$form['#pre_render'] = array();
}
if (!in_array('content_multigroup_node_form_pre_render', $form['#pre_render'])) {
array_unshift($form['#pre_render'], 'content_multigroup_node_form_pre_render');
}
}
if (($add_more = content_multigroup_add_more($form, $form_state, $group)) !== FALSE) {
$form[$group_name] += $add_more;
}
}