You are here

public function FlexiformEntityFormDisplayEditForm::form in Flexiform 8

Gets the actual form array to be built.

Overrides EntityDisplayFormBase::form

See also

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

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

File

src/Form/FlexiformEntityFormDisplayEditForm.php, line 30

Class

FlexiformEntityFormDisplayEditForm
Provides Flexiform form elements for the EntityFormDisplay entity type.

Namespace

Drupal\flexiform\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  if (!isset($form['#parents'])) {
    $form['#parents'] = [];
  }
  $form_state = MultipleEntityFormState::createForForm($form, $form_state);
  $form = parent::form($form, $form_state);
  $form['#entity_type'] = $this->entity
    ->getTargetEntityTypeId();
  $form['#bundle'] = $this->entity
    ->getTargetBundle();
  $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
  $form['#attached']['library'][] = 'core/drupal.ajax';
  $form['fields']['#header'][0] = $this
    ->t('Component');

  // Components.
  $component_type_manager = \Drupal::service('plugin.manager.flexiform.form_component_type');
  $component_rows = [];
  foreach ($component_type_manager
    ->getDefinitions() as $component_type => $definition) {
    $component_rows += $component_type_manager
      ->createInstance($component_type)
      ->setFormDisplay($this->entity)
      ->setFormEntityManager($this->entity
      ->getFormEntityManager($form_state))
      ->componentRows($this, $form, $form_state);
  }
  $form['fields'] = $component_rows + $form['fields'];

  // Enhancers.
  $form['enhancer'] = [
    '#type' => 'vertical_tabs',
  ];
  foreach ($this->entity
    ->getFormEnhancers('configuration_form') as $enhancer_name => $enhancer) {
    if ($enhancer instanceof ConfigurableFormEnhancerInterface) {
      $form['enhancer_' . $enhancer_name] = [
        '#type' => 'details',
        '#title' => $enhancer
          ->getPluginDefinition()['label'],
        '#parents' => [
          'enhancer',
          $enhancer_name,
        ],
        '#array_parents' => [
          'enhancer_' . $enhancer_name,
        ],
        '#group' => 'enhancer',
        '#tree' => TRUE,
      ];
      $form['enhancer_' . $enhancer_name] += $enhancer
        ->configurationForm($form['enhancer_' . $enhancer_name], $form_state);
    }
  }
  if (!empty($form['modes'])) {
    $form['modes']['#weight'] = 90;
  }
  return $form;
}