function AjaxTestForm::buildForm

Same name and namespace in other branches
  1. 9 core/modules/system/tests/modules/ajax_test/src/Form/AjaxTestForm.php \Drupal\ajax_test\Form\AjaxTestForm::buildForm()
  2. 8.9.x core/modules/system/tests/modules/ajax_test/src/Form/AjaxTestForm.php \Drupal\ajax_test\Form\AjaxTestForm::buildForm()
  3. 11.x core/modules/system/tests/modules/ajax_test/src/Form/AjaxTestForm.php \Drupal\ajax_test\Form\AjaxTestForm::buildForm()

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

core/modules/system/tests/modules/ajax_test/src/Form/AjaxTestForm.php, line 28

Class

AjaxTestForm
Dummy form for testing DialogRenderer with _form routes.

Namespace

Drupal\ajax_test\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['#action'] = Url::fromRoute('ajax_test.dialog')->toString();
  $form['description'] = [
    '#markup' => '<p>' . $this->t("Ajax Form contents description.") . '</p>',
  ];
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this->t('Do it'),
  ];
  $form['actions']['preview'] = [
    '#title' => 'Preview',
    '#type' => 'link',
    '#url' => Url::fromRoute('ajax_test.dialog_form'),
    '#attributes' => [
      'class' => [
        'use-ajax',
        'button',
      ],
      'data-dialog-type' => 'modal',
    ],
  ];
  $form['actions']['hello_world'] = [
    '#type' => 'submit',
    '#value' => $this->t('Hello world'),
    // No regular submit-handler. This form only works via JavaScript.
'#submit' => [],
    '#ajax' => [
      'callback' => '::helloWorld',
      'event' => 'click',
    ],
  ];
  return $form;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.