You are here

function field_group_field_overview_submit in Field Group 8

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

Submit handler for the overview screens.

Parameters

Array $form The complete form.:

FormStateInterface $form_state The state of the form.:

1 string reference to 'field_group_field_overview_submit'
field_group_field_ui_display_form_alter in includes/field_ui.inc
Function to alter the display overview screens.

File

includes/field_ui.inc, line 383
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, FormStateInterface $form_state) {
  $form_values = $form_state
    ->getValue('fields');
  $fields_added = $form_state
    ->getValue('fields_added');

  /**
   * @var \Drupal\Core\Entity\EntityDisplayBase $display
   */
  $display = $form['#context'];
  $entity_type = $display
    ->get('targetEntityType');
  $bundle = $display
    ->get('bundle');
  $mode = $display
    ->get('mode');
  $context = field_group_get_context_from_display($display);

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

  // Update existing groups.
  $groups = field_group_info_groups($entity_type, $bundle, $context, $mode, TRUE);
  $field_group_form_state = $form_state
    ->get('field_group');
  if (!empty($field_group_form_state)) {
    foreach ($form['#fieldgroups'] as $group_name) {

      // Only save updated groups.
      if (!isset($field_group_form_state[$group_name])) {
        continue;
      }
      $group = $groups[$group_name];
      $group->label = $field_group_form_state[$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($field_group_form_state[$group_name]->format_settings)) {
        $group->format_settings = $field_group_form_state[$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) {
        $default_formatter_settings = _field_group_get_default_formatter_settings($group->format_type, $context);
        $group->format_settings += $default_formatter_settings;
      }

      /** @var EntityFormInterface $entity_form */
      $entity_form = $form_state
        ->getFormObject();

      /** @var EntityDisplayInterface $display */
      $display = $entity_form
        ->getEntity();
      field_group_group_save($group, $display);
    }
  }
  \Drupal::cache()
    ->invalidate('field_groups');
}