You are here

function field_group_field_overview_submit in Field Group 7

Same name and namespace in other branches
  1. 8.3 includes/field_ui.inc \field_group_field_overview_submit()
  2. 8 includes/field_ui.inc \field_group_field_overview_submit()
  3. 7.2 field_group.field_ui.inc \field_group_field_overview_submit()

Submit handler for the overview screens.

Parameters

Array $form The complete form.:

Array $form_state The state of the form.:

1 string reference to 'field_group_field_overview_submit'
field_group_field_ui_overview_form_alter in ./field_group.field_ui.inc
Function to alter the fields overview and display overview screen.

File

./field_group.field_ui.inc, line 589
Field_group.field_ui.inc is a file that contains most functions needed on the Fields UI Manage forms (display and fields).

Code

function field_group_field_overview_submit($form, &$form_state) {
  $form_values = $form_state['values']['fields'];
  $entity_type = $form['#entity_type'];
  $bundle = $form['#bundle'];
  $mode = isset($form['#view_mode']) ? $form['#view_mode'] : 'form';

  // Collect children.
  $children = array_fill_keys($form['#groups'], array());
  foreach ($form_values as $name => $value) {
    if (!empty($value['parent'])) {

      // Substitute newly added fields, in case they were dragged
      // directly in a group.
      if ($name == '_add_new_field' && isset($form_state['fields_added']['_add_new_field'])) {
        $name = $form_state['fields_added']['_add_new_field'];
      }
      elseif ($name == '_add_existing_field' && isset($form_state['fields_added']['_add_existing_field'])) {
        $name = $form_state['fields_added']['_add_existing_field'];
      }
      $children[$value['parent']][$name] = $name;
    }
  }

  // Prepare storage with ctools.
  ctools_include('export');

  // Create new group.
  if (!empty($form_values['_add_new_group']['group_name'])) {
    $values = $form_values['_add_new_group'];
    $field_group_types = field_group_formatter_info();
    $formatter = $field_group_types[$mode == 'form' ? 'form' : 'display'][$values['format']['type']];
    $new_group = (object) array(
      'identifier' => $values['group_name'] . '|' . $entity_type . '|' . $bundle . '|' . $mode,
      'group_name' => $values['group_name'],
      'entity_type' => $entity_type,
      'bundle' => $bundle,
      'mode' => $mode,
      'children' => isset($children['_add_new_group']) ? array_keys($children['_add_new_group']) : array(),
      'parent_name' => $values['parent'],
      'weight' => $values['weight'],
      'label' => $values['label'],
      'format_type' => $values['format']['type'],
      'disabled' => FALSE,
    );
    $new_group->format_settings = array(
      'formatter' => isset($formatter['default_formatter']) ? $formatter['default_formatter'] : '',
    );
    if (isset($formatter['instance_settings'])) {
      $new_group->format_settings['instance_settings'] = $formatter['instance_settings'];
    }
    $classes = _field_group_get_html_classes($new_group);
    $new_group->format_settings['instance_settings']['classes'] = implode(' ', $classes->optional);

    // Save and enable it in ctools.
    ctools_export_crud_save('field_group', $new_group);
    ctools_export_crud_enable('field_group', $new_group->identifier);

    // Store new group information for any additional submit handlers.
    $form_state['groups_added']['_add_new_group'] = $new_group->group_name;
    drupal_set_message(t('New group %label successfully created.', array(
      '%label' => $new_group->label,
    )));

    // Replace the newly created group in the $children array, in case it was
    // dragged directly in an existing field.
    foreach (array_keys($children) as $parent) {
      if (isset($children[$parent]['_add_new_group'])) {
        unset($children[$parent]['_add_new_group']);
        $children[$parent][$new_group->group_name] = $new_group->group_name;
      }
    }
  }

  // Update existing groups.
  $groups = field_group_info_groups($entity_type, $bundle, $mode, TRUE);
  foreach ($form['#groups'] as $group_name) {
    $group = $groups[$group_name];
    $group->label = $form_state['field_group'][$group_name]->label;
    $group->children = array_keys($children[$group_name]);
    $group->parent_name = $form_values[$group_name]['parent'];
    $group->weight = $form_values[$group_name]['weight'];
    $old_format_type = $group->format_type;
    $group->format_type = isset($form_values[$group_name]['format']['type']) ? $form_values[$group_name]['format']['type'] : 'visible';
    if (isset($form_state['field_group'][$group_name]->format_settings)) {
      $group->format_settings = $form_state['field_group'][$group_name]->format_settings;
    }

    // If the format type is changed, make sure we have all required format settings.
    if ($group->format_type != $old_format_type) {
      $mode = $group->mode == 'form' ? 'form' : 'display';
      $default_formatter_settings = _field_group_get_default_formatter_settings($group->format_type, $mode);
      $group->format_settings += $default_formatter_settings;
      $group->format_settings['instance_settings'] += $default_formatter_settings['instance_settings'];
    }
    ctools_export_crud_save('field_group', $group);
  }
  cache_clear_all('field_groups', 'cache_field');
}