You are here

public function FormEntityAddForm::buildForm in Flexiform 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 FormEntityBaseForm::buildForm

File

src/Form/FormEntityAddForm.php, line 28

Class

FormEntityAddForm
Provides a form for adding new entity forms.

Namespace

Drupal\flexiform\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, FlexiformEntityFormDisplayInterface $form_display = NULL) {
  $form_state = MultipleEntityFormState::createForForm($form, $form_state);
  parent::buildForm($form, $form_state, $form_display);
  $available_plugins = $this->pluginManager
    ->getDefinitionsForContexts($this
    ->formEntityManager($form_state)
    ->getContexts());

  // Add prefix and suffix for ajax purposes.
  $form['#prefix'] = '<div id="flexiform-form-entity-add-wrapper">';
  $form['#suffix'] = '</div>';
  if ($plugin_id = $form_state
    ->get('selected_form_entity')) {
    $plugin = $this->pluginManager
      ->createInstance($plugin_id, [
      'manager' => $this
        ->formEntityManager($form_state),
    ]);
    return $this
      ->buildConfigurationForm($form, $form_state, $plugin);
  }
  else {

    // Prepare selector form.
    $plugin_options = [];
    foreach ($available_plugins as $plugin_id => $plugin_definition) {
      if (empty($plugin_definition['no_ui'])) {
        $plugin_options[$plugin_id] = $plugin_definition['label'];
      }
    }
    $form['form_entity'] = [
      '#type' => 'select',
      '#required' => TRUE,
      '#options' => $plugin_options,
      '#title' => $this
        ->t('Form Entity'),
    ];
    $form['actions'] = [
      '#type' => 'actions',
      'submit' => [
        '#type' => 'submit',
        '#value' => $this
          ->t('Continue'),
        '#submit' => [
          [
            $this,
            'submitSelectPlugin',
          ],
        ],
        '#ajax' => [
          'callback' => [
            $this,
            'ajaxSubmit',
          ],
          'event' => 'click',
        ],
      ],
    ];
  }
  return $form;
}