You are here

function field_group_field_ui_display_form_alter in Field Group 8.3

Same name and namespace in other branches
  1. 8 includes/field_ui.inc \field_group_field_ui_display_form_alter()

Function to alter the display overview screens.

2 calls to field_group_field_ui_display_form_alter()
field_group_form_entity_form_display_edit_form_alter in ./field_group.module
Implements hook_form_FORM_ID_alter(). Using hook_form_field_ui_form_display_overview_form_alter.
field_group_form_entity_view_display_edit_form_alter in ./field_group.module
Implements hook_form_FORM_ID_alter(). Using hook_form_field_ui_display_overview_form_alter.

File

includes/field_ui.inc, line 102
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_ui_display_form_alter(&$form, FormStateInterface $form_state) {

  // Only start altering the form if we need to.
  if (empty($form['#fields']) && empty($form['#extra'])) {
    return;
  }
  $entity_display_form = $form_state
    ->getBuildInfo()['callback_object'];
  if (!$entity_display_form instanceof EntityDisplayFormBase) {
    throw new InvalidArgumentException('Unknown callback object.');
  }
  $display = $entity_display_form
    ->getEntity();
  $params = field_group_field_ui_form_params($form, $display);
  $form['#fieldgroups'] = array_keys($params->groups);
  $form['#context'] = $display;
  $table =& $form['fields'];
  $form_state_values = $form_state
    ->getValues();
  $field_group_form_state = $form_state
    ->get('field_group');
  if ($field_group_form_state == NULL) {
    $field_group_form_state = $params->groups;
  }
  $table['#parent_options'] = [];

  // Extend available parenting options.
  foreach ($field_group_form_state as $name => $group) {
    $table['#parent_options'][$name] = $group->label;
  }

  // Update existing rows accordingly to the parents.
  foreach (Element::children($table) as $name) {
    $table[$name]['parent_wrapper']['parent']['#options'] = $table['#parent_options'];

    // Inherit the value of the parent when default value is empty.
    if (empty($table[$name]['parent_wrapper']['parent']['#default_value'])) {
      $table[$name]['parent_wrapper']['parent']['#default_value'] = isset($params->parents[$name]) ? $params->parents[$name] : '';
    }
  }
  $formatter_options = FormatterHelper::formatterOptions($params->context);
  $refresh_rows = isset($form_state_values['refresh_rows']) ? $form_state_values['refresh_rows'] : (isset($form_state
    ->getUserInput()['refresh_rows']) ? $form_state
    ->getUserInput()['refresh_rows'] : NULL);

  // Create the group rows and check actions.
  foreach ($form['#fieldgroups'] as $name) {
    $group =& $field_group_form_state[$name];

    // Check the currently selected formatter, and merge persisted values for
    // formatter settings for the group.
    // This needs to be done first, so all fields are updated before creating form elements.
    if (isset($refresh_rows) && $refresh_rows == $name) {
      $settings = isset($form_state_values['fields'][$name]) ? $form_state_values['fields'][$name] : (isset($form_state
        ->getUserInput()['fields'][$name]) ? $form_state
        ->getUserInput()['fields'][$name] : NULL);
      if (array_key_exists('settings_edit', $settings)) {
        $group = $field_group_form_state[$name];
      }
      field_group_formatter_row_update($group, $settings);
    }

    // Save the group when the configuration is submitted.
    if (!empty($form_state_values[$name . '_plugin_settings_update'])) {
      field_group_formatter_settings_update($group, $form_state_values['fields'][$name]);
    }

    // After all updates are finished, let the form_state know.
    $field_group_form_state[$name] = $group;
    $settings = field_group_format_settings_form($group, $form, $form_state);
    $id = strtr($name, '_', '-');

    // A group cannot be selected as its own parent.
    $parent_options = $table['#parent_options'];
    $region = isset($group->region) && in_array($group->region, $params->available_regions) ? $group->region : $params->default_region;
    unset($parent_options[$name]);
    $table[$name] = [
      '#attributes' => [
        'class' => [
          'draggable',
          'field-group',
        ],
        'id' => $id,
      ],
      '#row_type' => 'group',
      '#region_callback' => 'field_group_display_overview_row_region',
      '#js_settings' => [
        'rowHandler' => 'group',
      ],
      'human_name' => [
        '#markup' => $group->label,
        '#prefix' => '<span class="group-label">',
        '#suffix' => '</span>',
      ],
      'weight' => [
        '#type' => 'textfield',
        '#default_value' => $group->weight,
        '#size' => 3,
        '#attributes' => [
          'class' => [
            'field-weight',
          ],
        ],
      ],
      'parent_wrapper' => [
        'parent' => [
          '#type' => 'select',
          '#options' => $parent_options,
          '#empty_value' => '',
          '#default_value' => isset($params->parents[$name]) ? $params->parents[$name] : '',
          '#attributes' => [
            'class' => [
              'field-parent',
            ],
          ],
          '#parents' => [
            'fields',
            $name,
            'parent',
          ],
        ],
        'hidden_name' => [
          '#type' => 'hidden',
          '#default_value' => $name,
          '#attributes' => [
            'class' => [
              'field-name',
            ],
          ],
        ],
      ],
      'region' => [
        '#type' => 'select',
        '#options' => $entity_display_form
          ->getRegionOptions(),
        '#default_value' => $region,
        '#attributes' => [
          'class' => [
            'field-region',
          ],
        ],
      ],
    ];

    // For view settings. Add a spacer cell. We can't use colspan because of the javascript .
    if ($params->context == 'view') {
      $table[$name] += [
        'spacer' => [
          '#markup' => '&nbsp;',
        ],
      ];
    }
    $table[$name] += [
      'format' => [
        'type' => [
          '#type' => 'select',
          '#options' => $formatter_options,
          '#default_value' => $group->format_type,
          '#attributes' => [
            'class' => [
              'field-group-type',
            ],
          ],
        ],
      ],
    ];
    $base_button = [
      '#submit' => [
        [
          $form_state
            ->getBuildInfo()['callback_object'],
          'multistepSubmit',
        ],
      ],
      '#ajax' => [
        'callback' => [
          $form_state
            ->getBuildInfo()['callback_object'],
          'multistepAjax',
        ],
        'wrapper' => 'field-display-overview-wrapper',
        'effect' => 'fade',
      ],
      '#field_name' => $name,
    ];
    if ($form_state
      ->get('plugin_settings_edit') == $name) {
      $table[$name]['format']['#cell_attributes'] = [
        'colspan' => 2,
      ];
      $table[$name]['format']['format_settings'] = [
        '#type' => 'container',
        '#attributes' => [
          'class' => [
            'field-plugin-settings-edit-form',
          ],
        ],
        '#parents' => [
          'fields',
          $name,
          'settings_edit_form',
        ],
        '#weight' => -5,
        'label' => [
          '#markup' => t('Field group format:') . ' <span class="formatter-name">' . $group->format_type . '</span>',
        ],
        // Create a settings form where hooks can pick in.
        'settings' => $settings,
        'actions' => [
          '#type' => 'actions',
          'save_settings' => $base_button + [
            '#type' => 'submit',
            '#name' => $name . '_plugin_settings_update',
            '#value' => t('Update'),
            '#op' => 'update',
          ],
          'cancel_settings' => $base_button + [
            '#type' => 'submit',
            '#name' => $name . '_plugin_settings_cancel',
            '#value' => t('Cancel'),
            '#op' => 'cancel',
            // Do not check errors for the 'Cancel' button.
            '#limit_validation_errors' => [],
          ],
        ],
      ];
      $table[$name]['#attributes']['class'][] = 'field-formatter-settings-editing';
      $table[$name]['format']['type']['#attributes']['class'] = [
        'visually-hidden',
      ];
    }
    else {

      // After saving, the settings are updated here aswell. First we create
      // the element for the table cell.
      $table[$name]['settings_summary'] = [
        '#markup' => '',
      ];
      if (!empty($group->format_settings)) {
        $table[$name]['settings_summary'] = field_group_format_settings_summary($name, $group);
      }

      // Add the configure button.
      $table[$name]['settings_edit'] = $base_button + [
        '#type' => 'image_button',
        '#name' => $name . '_group_settings_edit',
        '#src' => 'core/misc/icons/787878/cog.svg',
        '#attributes' => [
          'class' => [
            'field-plugin-settings-edit',
          ],
          'alt' => t('Edit'),
        ],
        '#op' => 'edit',
        // Do not check errors for the 'Edit' button, but make sure we get
        // the value of the 'plugin type' select.
        '#limit_validation_errors' => [
          [
            'fields',
            $name,
            'type',
          ],
        ],
        '#prefix' => '<div class="field-plugin-settings-edit-wrapper">',
        '#suffix' => '</div>',
      ];
      $delete_route = FieldgroupUi::getDeleteRoute($group);
      $table[$name]['settings_edit']['#suffix'] .= Link::fromTextAndUrl(t('delete'), $delete_route)
        ->toString();
    }
    $form_state
      ->set('field_group', $field_group_form_state);
  }

  // Additional row: add new group.
  $parent_options = $table['#parent_options'];
  $form['#attached']['library'][] = 'field_group/field_ui';
  array_unshift($form['actions']['submit']['#submit'], 'field_group_field_overview_submit');

  // Create the settings for fieldgroup as vertical tabs (merged with DS).
  field_group_field_ui_create_vertical_tabs($form, $form_state, $params);

  // Show a warning if the user has not set up required containers.
  if ($form['#fieldgroups']) {
    $parent_requirements = [
      'accordion-item' => [
        'parent' => 'accordion',
        'message' => 'Each Accordion item element needs to have a parent Accordion group element.',
      ],
    ];

    // On display overview tabs need to be checked.
    if (field_group_get_context_from_display($display) == 'view') {
      $parent_requirements['tab'] = [
        'parent' => 'tabs',
        'message' => 'Each tab element needs to have a parent tabs group element.',
      ];
    }
    foreach ($form['#fieldgroups'] as $group_name) {
      $group_check = field_group_load_field_group($group_name, $params->entity_type, $params->bundle, $params->context, $params->mode);
      if (isset($parent_requirements[$group_check->format_type])) {
        if (!$group_check->parent_name || field_group_load_field_group($group_check->parent_name, $params->entity_type, $params->bundle, $params->context, $params->mode)->format_type != $parent_requirements[$group_check->format_type]['parent']) {
          \Drupal::messenger()
            ->addMessage(t($parent_requirements[$group_check->format_type]['message']), 'warning', FALSE);
        }
      }
    }
  }
}