You are here

function content_multigroup_field_overview_form_validate in Content Construction Kit (CCK) 6.3

Validation for creating/moving fields and groups on the Manage Fields screen.

1 string reference to 'content_multigroup_field_overview_form_validate'
content_multigroup_form_alter in modules/content_multigroup/content_multigroup.module
Implementation of hook_form_alter().

File

modules/content_multigroup/content_multigroup.admin.inc, line 22
Implementation of node type administration functions for content multigroup.

Code

function content_multigroup_field_overview_form_validate($form, &$form_state) {
  $form_values = $form_state['values'];
  $type_name = $form['#type_name'];
  $fields = array();
  $groups = array();
  $group = $form_values['_add_new_group'];
  if (array_filter(array(
    $group['label'],
    $group['group_name'],
  ))) {
    $group['settings'] = field_group_default_settings($group['group_type']);
    $validation = fieldgroup_validate_name($group, $form['#type_name']);

    // If there's something wrong with the new group,
    // don't bother doing any more validation, further
    // processing will be stopped by the fieldgroup module.
    if (!empty($validation['errors'])) {
      return;
    }
    $group['group_name'] = $validation['group_name'];
    $new_group_name = $group['group_name'];
    $groups['_add_new_group'] = $group;
  }

  // See if we have fields moving into or out of a Multigroup.
  // Set any fields to use the new name here so they will get processed
  // correctly by the fieldgroup module when saved.
  $group_rows = array();
  foreach ($form_values as $key => $values) {
    if ($values['parent'] == '_add_new_group') {
      $values['parent'] = $new_group_name;
      $form_values[$key] = $values;
    }
    if (!empty($form[$key]['#row_type']) && $form[$key]['#row_type'] == 'group') {

      // Gather up info about all groups.
      $group_name = $form_values[$key]['group']['group_name'];
      $groups[$group_name] = $form_values[$key]['group'];
      $group_rows[$group_name] = $group_name;
    }
    if (!empty($form[$key]['#row_type']) && $form[$key]['#row_type'] == 'field') {
      if ($values['prev_parent'] != $values['parent']) {

        // Gather up fields that have moved in or out of a group.
        $fields[$key] = $form_values[$key]['field'];
      }
    }
  }
  $rebuild = FALSE;

  // Test that a group was not moved into a multigroup, an invalid combination.
  foreach ($groups as $key => $values) {
    if (in_array($key, $group_rows)) {
      $parent = $form_values[$key]['parent'];
      $parent_info = !empty($parent) ? $form_values[$parent] : array();
      if (!empty($parent) && $parent_info['group']['group_type'] == 'multigroup') {
        $error_message = t('You cannot place any kind of group inside a multigroup. The group @name was moved back to where it started.', array(
          '@name' => $key,
        ));
        form_set_value($form[$key]['weight'], $form[$key]['weight']['#default_value'], $form_state);
        form_set_value($form[$key]['parent'], $form[$key]['parent']['#default_value'], $form_state);
        drupal_set_message($error_message, 'error');
      }
    }
  }

  // Synchronize the multiple value values for all fields in a group, they must be the same.
  // Also ensure that fields moved into multigroups are fields that are allowed.
  // In some cases, it may not be safe to move a field back out of a multigroup because
  // it will behave differently elsewhere, so check that too.
  foreach ($fields as $field_name => $field) {
    $new_group = $form_values[$field_name]['parent'];
    $old_group = $form_values[$field_name]['prev_parent'];
    if (!empty($new_group) && isset($groups[$new_group]) && $groups[$new_group]['group_type'] == 'multigroup') {
      $allowed_in = content_multigroup_allowed_in($field, $groups[$new_group]);
      if (!$allowed_in['allowed']) {
        form_set_error($field_name, $allowed_in['message']);
      }
      else {
        if (!empty($allowed_in['message'])) {
          drupal_set_message($allowed_in['message']);
        }
        module_load_include('inc', 'content', 'includes/content.crud');
        $content_type = content_types($type_name);
        $group_multiple = $groups[$new_group]['settings']['multigroup']['multiple'];
        $multiple_values = content_multigroup_multiple_values();
        $field = $content_type['fields'][$field_name];
        $field['multiple'] = $group_multiple;
        $field = content_field_instance_collapse($field);
        content_field_instance_update($field, FALSE);
        $rebuild = TRUE;
        drupal_set_message(t('The field %field has been updated to use %multiple values, to match the multiple value setting of the Multigroup %group.', array(
          '%field' => $field['label'],
          '%multiple' => $multiple_values[$group_multiple],
          '%group' => $groups[$new_group]['label'],
        )));
      }
    }
    elseif (!empty($old_group) && isset($groups[$old_group]) && $groups[$old_group]['group_type'] == 'multigroup') {
      $allowed_out = content_multigroup_allowed_out($field, $groups[$old_group]);
      if (!$allowed_out['allowed']) {
        form_set_error($field_name, $allowed_out['message']);
      }
      elseif (!empty($allowed_out['message'])) {
        drupal_set_message($allowed_out['message']);
      }
    }
  }

  // Clear caches and rebuild menu only if any field has been updated.
  if ($rebuild) {
    content_clear_type_cache(TRUE);
    menu_rebuild();
  }
}