You are here

function content_multigroup_group_form in Content Construction Kit (CCK) 6.3

Create a new delta value for the group.

Called in form_alter and by AHAH add more.

2 calls to content_multigroup_group_form()
content_multigroup_add_more_js in modules/content_multigroup/content_multigroup.node_form.inc
Menu callback for AHAH addition of new empty widgets.
_content_multigroup_fieldgroup_form in modules/content_multigroup/content_multigroup.node_form.inc
Implementation of hook_fieldgroup_form().

File

modules/content_multigroup/content_multigroup.node_form.inc, line 151
Implementation of node edit functions for content multigroup.

Code

function content_multigroup_group_form(&$form, &$form_state, $group, $delta) {
  module_load_include('inc', 'content', 'includes/content.node_form');
  $element = array();
  $type_name = $group['type_name'];
  $content_type = content_types($type_name);
  $group_name = $group['group_name'];
  if (!isset($form[$group_name])) {

    //nested AHAH, not initial build
    $element[$group_name] = array_shift(content_get_nested_elements($form, $group_name));
  }
  else {

    //initial build (via content_multigroup_fieldgroup_form) or non-nested AHAH
    $element[$group_name] = $form[$group_name];
  }
  if ($group['group_type'] != 'multigroup' || !empty($element[$group['group_name']]['#access']) && $element[$group['group_name']]['#access'] != TRUE || empty($element[$group['group_name']])) {
    return;
  }
  $group_fields = $form['#multigroups'][$group_name];
  $element[$group_name]['#fields'] = array_keys($group_fields);
  $node = $form['#node'];
  $group_multiple = $group['settings']['multigroup']['multiple'];
  foreach ($group_fields as $field_name => $field) {
    if (empty($element[$group_name][$delta])) {
      $element[$group_name] += array(
        $delta => array(
          $field_name => array(),
        ),
      );
    }
    else {
      $element[$group_name][$delta][$field_name] = array();
    }
    $item_count = isset($form_state['item_count'][$group_name]) ? $form_state['item_count'][$group_name] : $element[$group_name]['#item_count'];
    $element[$group_name][$delta]['_weight'] = array(
      '#type' => 'weight',
      '#delta' => $item_count,
      // this 'delta' is the 'weight' element's property
      '#default_value' => $delta,
      '#weight' => 100,
    );

    // Add a checkbox to allow users remove a single delta subgroup.
    // See content_set_empty() and theme_content_multigroup_node_form().
    if ($group_multiple == 1) {
      $element[$group_name][$delta]['_remove'] = array(
        '#type' => 'checkbox',
        '#attributes' => array(
          'class' => 'content-multiple-remove-checkbox',
        ),
        '#default_value' => isset($form_state['multigroup_removed'][$group_name][$delta]) ? $form_state['multigroup_removed'][$group_name][$delta] : 0,
      );
    }

    // Make each field into a pseudo single value field
    // with the right delta value.
    $field['multiple'] = 0;
    $form['#field_info'][$field_name] = $field;
    $node_copy = drupal_clone($node);

    // Set the form '#node' to the delta value we want so the Content
    // module will feed the right $items to the field module in
    // content_field_form().
    // There may be missing delta values for fields that were
    // never created, so check first.
    if (!empty($node->{$field_name}) && isset($node->{$field_name}[$delta])) {
      $node_copy->{$field_name} = array(
        $delta => $node->{$field_name}[$delta],
      );
    }
    else {
      $value = NULL;

      // Try to obtain default values only if the node is being created.
      if (!isset($node->nid) && content_callback('widget', 'default value', $field) != CONTENT_CALLBACK_NONE) {

        // If a module wants to insert custom default values here,
        // it should provide a hook_default_value() function to call,
        // otherwise the content module's content_default_value() function
        // will be used.
        $callback = content_callback('widget', 'default value', $field) == CONTENT_CALLBACK_CUSTOM ? $field['widget']['module'] . '_default_value' : 'content_default_value';
        if (function_exists($callback)) {
          $items = $callback($form, $form_state, $field, 0);
          $value = !empty($items) ? $items[0] : '';
        }
      }
      $node_copy->{$field_name} = array(
        $delta => $value,
      );
    }
    $form['#node'] = $node_copy;

    // Place the new element into the $delta position in the group form.
    if (content_handle('widget', 'multiple values', $field) == CONTENT_HANDLE_CORE) {
      $field_form = content_field_form($form, $form_state, $field, $delta);
      $value = array_key_exists($delta, $field_form[$field_name]) ? $delta : 0;
      $element[$group_name][$delta][$field_name] = $field_form[$field_name][$value];
    }
    else {

      // When the form is submitted, get the element data from the form values.
      if (isset($form_state['values'][$field_name])) {
        $form_state_copy = $form_state;
        if (isset($form_state_copy['values'][$field_name][$delta])) {
          $form_state_copy['values'][$field_name] = array(
            $delta => $form_state_copy['values'][$field_name][$delta],
          );
        }
        else {
          $form_state_copy['values'][$field_name] = array(
            $delta => NULL,
          );
        }
        $field_form = content_field_form($form, $form_state_copy, $field, $delta);
      }
      else {
        $field_form = content_field_form($form, $form_state, $field, $delta);
      }

      // Multiple value fields have an additional level in the array form that
      // needs to get fixed in $form_state['values'].
      if (!isset($field_form[$field_name]['#element_validate'])) {
        $field_form[$field_name]['#element_validate'] = array();
      }
      $field_form[$field_name]['#element_validate'][] = 'content_multigroup_fix_multivalue_fields';
      $element[$group_name][$delta][$field_name] = $field_form[$field_name];
    }
    $element[$group_name][$delta][$field_name]['#weight'] = $field['widget']['weight'];
  }

  // Reset the form '#node' back to its original value.
  $form['#node'] = $node;
  return $element;
}