You are here

public function FormComponentAddForm::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 FormInterface::buildForm

File

src/Form/FormComponentAddForm.php, line 90

Class

FormComponentAddForm
Provides a form for adding new form components.

Namespace

Drupal\flexiform\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, FlexiformEntityFormDisplayInterface $form_display = NULL, $component_type = '') {
  $this->formDisplay = $form_display;
  $this->componentType = $this->pluginManager
    ->createInstance($component_type);
  $this->componentType
    ->setFormDisplay($form_display);
  $form['admin_label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Administration Label'),
    '#description' => $this
      ->t('A label for this component. This will only be used administrativly'),
  ];
  $form['name'] = [
    '#type' => 'machine_name',
    '#title' => $this
      ->t('Component Name'),
    '#description' => $this
      ->t('The name of this component. This must be unique in the form.'),
    '#machine_name' => [
      'source' => [
        'admin_label',
      ],
      'exists' => [
        $this,
        'nameExists',
      ],
    ],
  ];
  $form['region'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Region'),
    '#options' => $this->formDisplay
      ->getRegionOptions(),
  ];
  unset($form['region']['#options']['hidden']);
  $form['options'] = [
    '#type' => 'container',
    '#tree' => TRUE,
    '#parents' => [
      'options',
    ],
  ];
  $form['options'] += $this->componentType
    ->addComponentForm($form['options'], $form_state);
  $form['actions'] = [
    '#type' => 'actions',
    'submit' => [
      '#type' => 'submit',
      '#value' => $this
        ->t('Add Component'),
    ],
  ];
  return $form;
}