You are here

public function MultipleEntityForm::configurationForm in Flexiform 8

The configuration form.

Parameters

array $form: The form array.

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

Return value

array The form with any additions.

Overrides ConfigurableFormEnhancerInterface::configurationForm

File

src/Plugin/FormEnhancer/MultipleEntityForm.php, line 33

Class

MultipleEntityForm
Form enhancer for multiple entity forms.

Namespace

Drupal\flexiform\Plugin\FormEnhancer

Code

public function configurationForm(array $form, FormStateInterface $form_state) {

  // Prepare a link to add an entity to this form.
  $target_entity_type = $this->formDisplay
    ->get('targetEntityType');

  /** @var \Drupal\Core\Entity\EntityTypeInterface $target_entity_def */
  $target_entity_def = \Drupal::service('entity_type.manager')
    ->getDefinition($target_entity_type);
  $url_params = [
    'form_mode_name' => $this->formDisplay
      ->get('mode'),
  ];
  if ($target_entity_def
    ->get('bundle_entity_type')) {
    $url_params[$target_entity_def
      ->get('bundle_entity_type')] = $this->formDisplay
      ->get('bundle');
  }
  else {
    if ($target_entity_def
      ->hasKey('bundle')) {
      $url_params['bundle'] = $this->formDisplay
        ->get('bundle');
    }
  }
  $form['add'] = [
    '#type' => 'link',
    '#title' => $this
      ->t('Add Entity'),
    '#url' => Url::fromRoute("entity.entity_form_display.{$target_entity_type}.form_mode.form_entity_add", $url_params),
    '#attributes' => $this
      ->getAjaxButtonAttributes(),
    '#attached' => [
      'library' => [
        'core/drupal.ajax',
      ],
    ],
  ];
  $form['entities'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Entity'),
      $this
        ->t('Plugin'),
      $this
        ->t('Operations'),
    ],
    '#title' => t('Entities'),
    '#empty' => t('This form display has no entities yet.'),
  ];
  foreach ($this->formDisplay
    ->getFormEntityManager($form_state)
    ->getContexts() as $namespace => $context) {
    $operations = [];
    if (!empty($namespace)) {
      $operation_params = $url_params;
      $operation_params['entity_namespace'] = $namespace;
      $operations['edit'] = [
        'title' => $this
          ->t('Edit'),
        'weight' => 10,
        'url' => Url::fromRoute("entity.entity_form_display.{$target_entity_type}.form_mode.form_entity_edit", $operation_params),
        'attributes' => $this
          ->getAjaxButtonAttributes(),
      ];
    }
    $form['entities'][$namespace] = [
      'human_name' => [
        '#plain_text' => $context
          ->getContextDefinition()
          ->getLabel(),
      ],
      'plugin' => [
        '#plain_text' => $context
          ->getFormEntity()
          ->getLabel(),
      ],
      'operations' => [
        '#type' => 'operations',
        '#links' => $operations,
        '#attached' => [
          'library' => [
            'core/drupal.ajax',
          ],
        ],
      ],
    ];
  }
  return $form;
}