You are here

public function EntityForm::buildConfigurationForm in Entity Browser 8.2

Same name and namespace in other branches
  1. 8 modules/entity_form/src/Plugin/EntityBrowser/Widget/EntityForm.php \Drupal\entity_browser_entity_form\Plugin\EntityBrowser\Widget\EntityForm::buildConfigurationForm()

Implements PluginFormInterface::buildConfigurationForm().

Overrides WidgetBase::buildConfigurationForm

File

modules/entity_form/src/Plugin/EntityBrowser/Widget/EntityForm.php, line 150

Class

EntityForm
Provides entity form widget.

Namespace

Drupal\entity_browser_entity_form\Plugin\EntityBrowser\Widget

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildConfigurationForm($form, $form_state);
  $parents = [
    'table',
    $this
      ->uuid(),
    'form',
  ];
  $entity_type = $form_state
    ->hasValue(array_merge($parents, [
    'entity_type',
  ])) ? $form_state
    ->getValue(array_merge($parents, [
    'entity_type',
  ])) : $this->configuration['entity_type'];
  $bundle = $form_state
    ->hasValue(array_merge($parents, [
    'bundle',
    'select',
  ])) ? $form_state
    ->getValue(array_merge($parents, [
    'bundle',
    'select',
  ])) : $this->configuration['bundle'];
  $form_mode = $form_state
    ->hasValue(array_merge($parents, [
    'form_mode',
    'form_select',
  ])) ? $form_state
    ->hasValue(array_merge($parents, [
    'form_mode',
    'form_select',
  ])) : $this->configuration['form_mode'];
  $definitions = $this->entityTypeManager
    ->getDefinitions();
  $entity_types = array_combine(array_keys($definitions), array_map(function (EntityTypeInterface $item) {
    return $item
      ->getLabel();
  }, $definitions));
  $form['entity_type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Entity type'),
    '#options' => $entity_types,
    '#default_value' => $entity_type,
    '#ajax' => [
      'callback' => [
        $this,
        'updateFormElements',
      ],
    ],
  ];
  $bundles = [];
  if ($entity_type) {
    $definitions = $this->entityTypeBundleInfo
      ->getBundleInfo($entity_type);
    $bundles = array_map(function ($item) {
      return $item['label'];
    }, $definitions);
  }
  $form['bundle'] = [
    '#type' => 'container',
    'select' => [
      '#type' => 'select',
      '#title' => $this
        ->t('Bundle'),
      '#options' => $bundles,
      '#default_value' => $bundle,
    ],
    '#attributes' => [
      'id' => 'bundle-wrapper-' . $this
        ->uuid(),
    ],
  ];
  $form['form_mode'] = [
    '#type' => 'container',
    'form_select' => [
      '#type' => 'select',
      '#title' => $this
        ->t('Form mode'),
      '#default_value' => $form_mode,
      '#options' => $this->entityDisplayRepository
        ->getFormModeOptions($entity_type),
    ],
    '#attributes' => [
      'id' => 'form-mode-wrapper-' . $this
        ->uuid(),
    ],
  ];
  return $form;
}