You are here

public function EdgeEntityDisplaySettingsForm::buildForm in Apigee Edge 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/EdgeEntityDisplaySettingsForm.php, line 137

Class

EdgeEntityDisplaySettingsForm
Configuration form for Apigee entities display settings.

Namespace

Drupal\apigee_edge\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL) {
  $this->entityTypeId = $entity_type_id;

  /** @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */
  $entity_type = $this->entityTypeManager
    ->getDefinition($entity_type_id);
  $config = $this
    ->configFactory()
    ->get("apigee_edge.display_settings.{$entity_type_id}");
  $form['display_settings'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Configure the display for @label listing page.', [
      '@label' => $entity_type
        ->getPluralLabel(),
    ]),
    '#collapsible' => FALSE,
  ];
  $form['display_settings']['display_type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Type'),
    '#default_value' => $config
      ->get('display_type'),
    '#required' => TRUE,
    '#description' => $this
      ->t('Select the <em>Default</em> type to display a table of entities with links to entity operations. Select <em>Display mode</em> to configure a custom display.'),
    '#options' => [
      EdgeEntityListBuilder::DEFAULT_DISPLAY_TYPE => $this
        ->t('Default'),
      EdgeEntityListBuilder::VIEW_MODE_DISPLAY_TYPE => $this
        ->t('Display mode'),
    ],
  ];
  $form['display_settings']['display_mode_container'] = [
    '#type' => 'container',
    '#states' => [
      'visible' => [
        ':input[name="display_type"]' => [
          'value' => 'view_mode',
        ],
      ],
    ],
  ];
  $display_modes = [
    'default' => $this
      ->t('Default'),
  ];
  foreach ($this->entityDisplayRepository
    ->getViewModes($entity_type
    ->id()) as $name => $view_mode) {
    $display_modes[$name] = $view_mode['label'];
  }
  $form['display_settings']['display_mode_container']['view_mode'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Display mode'),
    '#default_value' => $config
      ->get('view_mode'),
    '#description' => $this
      ->t('Select the display mode.', [
      ':uri' => '#',
    ]),
    '#options' => $display_modes,
  ];
  if ($this->moduleHandler
    ->moduleExists('field_ui')) {
    $form['display_settings']['display_mode_container']['display_mode_help'] = [
      '#theme' => 'item_list',
      '#items' => [
        [
          '#markup' => $this
            ->t('<a href=":uri">Click here</a> to configure the display.', [
            ':uri' => Url::fromRoute("entity.entity_view_display.{$entity_type->id()}.default")
              ->toString(),
          ]),
        ],
        [
          '#markup' => $this
            ->t('<a href=":uri">Click here</a> to add a new display mode.', [
            ':uri' => Url::fromRoute('entity.entity_view_mode.collection')
              ->toString(),
          ]),
        ],
      ],
    ];
  }
  return parent::buildForm($form, $form_state);
}