You are here

public function LayouterForm::buildForm in Layouter - WYSIWYG layout templates 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/LayouterForm.php, line 51

Class

LayouterForm
Provides multistep ajax form for an layout choice.

Namespace

Drupal\layouter\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $textarea_id = NULL) {
  $step = $form_state
    ->get('step');
  if (!$step) {
    $step = 1;
    $form_state
      ->set('step', $step);
  }
  $form['#prefix'] = '<div id="layouter-form-wrapper" class="layouter-form">';
  $form['#sufix'] = '</div>';
  $form['errors'] = [];
  $button_label = '';
  if ($step == 1) {
    $options = [];
    foreach ($this->templates as $id => $params) {
      $options[$id] = $params['title']
        ->render();
    }
    $form['data']['type'] = [
      '#title' => $this
        ->t('Choose the layout'),
      '#type' => 'radios',
      '#options' => $options,
      '#required' => TRUE,
      '#after_build' => [
        '::processLayoutTypeRadios',
      ],
    ];
    $button_label = $this
      ->t('Next');
  }
  if ($step == 2) {
    $this
      ->buildLayouterFields($form, $form_state);
    $button_label = $this
      ->t('Submit');
  }
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $button_label,
    '#name' => 'submit_button',
    '#attributes' => [
      'class' => [
        'use-ajax-submit',
      ],
    ],
    '#ajax' => [
      'callback' => '::ajaxResponse',
    ],
  ];
  $form['actions']['cancel'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Cancel'),
    '#name' => 'cancel_button',
    '#submit' => [
      '::cancelForm',
    ],
    '#limit_validation_errors' => [],
    '#attributes' => [
      'class' => [
        'use-ajax-submit',
      ],
    ],
  ];
  return $form;
}