You are here

public function EntityDisplayFormBase::form in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/field_ui/src/Form/EntityDisplayFormBase.php \Drupal\field_ui\Form\EntityDisplayFormBase::form()
  2. 10 core/modules/field_ui/src/Form/EntityDisplayFormBase.php \Drupal\field_ui\Form\EntityDisplayFormBase::form()

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

1 call to EntityDisplayFormBase::form()
LayoutBuilderEntityViewDisplayForm::form in core/modules/layout_builder/src/Form/LayoutBuilderEntityViewDisplayForm.php
Gets the actual form array to be built.
1 method overrides EntityDisplayFormBase::form()
LayoutBuilderEntityViewDisplayForm::form in core/modules/layout_builder/src/Form/LayoutBuilderEntityViewDisplayForm.php
Gets the actual form array to be built.

File

core/modules/field_ui/src/Form/EntityDisplayFormBase.php, line 167

Class

EntityDisplayFormBase
Base class for EntityDisplay edit forms.

Namespace

Drupal\field_ui\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $field_definitions = $this
    ->getFieldDefinitions();
  $extra_fields = $this
    ->getExtraFields();
  $form += [
    '#entity_type' => $this->entity
      ->getTargetEntityTypeId(),
    '#bundle' => $this->entity
      ->getTargetBundle(),
    '#fields' => array_keys($field_definitions),
    '#extra' => array_keys($extra_fields),
  ];
  if (empty($field_definitions) && empty($extra_fields) && ($route_info = FieldUI::getOverviewRouteInfo($this->entity
    ->getTargetEntityTypeId(), $this->entity
    ->getTargetBundle()))) {
    $this
      ->messenger()
      ->addWarning($this
      ->t('There are no fields yet added. You can add new fields on the <a href=":link">Manage fields</a> page.', [
      ':link' => $route_info
        ->toString(),
    ]));
    return $form;
  }
  $table = [
    '#type' => 'field_ui_table',
    '#header' => $this
      ->getTableHeader(),
    '#regions' => $this
      ->getRegions(),
    '#attributes' => [
      'class' => [
        'field-ui-overview',
      ],
      'id' => 'field-display-overview',
    ],
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'field-weight',
      ],
      [
        'action' => 'match',
        'relationship' => 'parent',
        'group' => 'field-parent',
        'subgroup' => 'field-parent',
        'source' => 'field-name',
      ],
      [
        'action' => 'match',
        'relationship' => 'parent',
        'group' => 'field-region',
        'subgroup' => 'field-region',
        'source' => 'field-name',
      ],
    ],
  ];

  // Field rows.
  foreach ($field_definitions as $field_name => $field_definition) {
    $table[$field_name] = $this
      ->buildFieldRow($field_definition, $form, $form_state);
  }

  // Non-field elements.
  foreach ($extra_fields as $field_id => $extra_field) {
    $table[$field_id] = $this
      ->buildExtraFieldRow($field_id, $extra_field);
  }
  $form['fields'] = $table;

  // Custom display settings.
  if ($this->entity
    ->getMode() == 'default') {

    // Only show the settings if there is at least one custom display mode.
    $display_mode_options = $this
      ->getDisplayModeOptions();

    // Unset default option.
    unset($display_mode_options['default']);
    if ($display_mode_options) {
      $form['modes'] = [
        '#type' => 'details',
        '#title' => $this
          ->t('Custom display settings'),
      ];

      // Prepare default values for the 'Custom display settings' checkboxes.
      $default = [];
      if ($enabled_displays = array_filter($this
        ->getDisplayStatuses())) {
        $default = array_keys(array_intersect_key($display_mode_options, $enabled_displays));
      }
      natcasesort($display_mode_options);
      $form['modes']['display_modes_custom'] = [
        '#type' => 'checkboxes',
        '#title' => $this
          ->t('Use custom display settings for the following @display_context modes', [
          '@display_context' => $this->displayContext,
        ]),
        '#options' => $display_mode_options,
        '#default_value' => $default,
      ];

      // Provide link to manage display modes.
      $form['modes']['display_modes_link'] = $this
        ->getDisplayModesLink();
    }
  }

  // In overviews involving nested rows from contributed modules (i.e
  // field_group), the 'plugin type' selects can trigger a series of changes
  // in child rows. The #ajax behavior is therefore not attached directly to
  // the selects, but triggered by the client-side script through a hidden
  // #ajax 'Refresh' button. A hidden 'refresh_rows' input tracks the name of
  // affected rows.
  $form['refresh_rows'] = [
    '#type' => 'hidden',
  ];
  $form['refresh'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Refresh'),
    '#op' => 'refresh_table',
    '#submit' => [
      '::multistepSubmit',
    ],
    '#ajax' => [
      'callback' => '::multistepAjax',
      'wrapper' => 'field-display-overview-wrapper',
      'effect' => 'fade',
      // The button stays hidden, so we hide the Ajax spinner too. Ad-hoc
      // spinners will be added manually by the client-side script.
      'progress' => 'none',
    ],
    '#attributes' => [
      'class' => [
        'visually-hidden',
      ],
    ],
  ];
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#button_type' => 'primary',
    '#value' => $this
      ->t('Save'),
  ];
  $form['#attached']['library'][] = 'field_ui/drupal.field_ui';
  return $form;
}