You are here

function field_group_field_overview_submit in Field Group 8.3

Same name and namespace in other branches
  1. 8 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.

\Drupal\Core\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 428
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');

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

  // Load field layout info.
  $field_group_params = $form_state
    ->get('field_group_params');
  $layout_regions = $field_group_params->available_regions;
  $default_region = $field_group_params->default_region;

  // Collect children.
  $children = array_fill_keys($form['#fieldgroups'], []);
  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);
  $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;

      // Sometimes field UI freaks a bit if people drag to fast when switching
      // nested values which results in an endless loop, so cleanup first.
      // unset($children[$group_name][$form_values[$group_name]['parent']]);.
      $group->children = array_keys($children[$group_name]);
      $group->parent_name = $form_values[$group_name]['parent'];
      $group->weight = $form_values[$group_name]['weight'];

      // If region is changed, make sure the group ends up in an existing region.
      $group->region = !in_array($form_values[$group_name]['region'], $layout_regions) ? $default_region : $form_values[$group_name]['region'];
      $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) {
        $group->format_settings += $manager
          ->getDefaultSettings($group->format_type, $context);
      }

      /** @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');
}