You are here

public function SubmitDriven::buildForm in Examples for Developers 3.x

Same name and namespace in other branches
  1. 8 ajax_example/src/Form/SubmitDriven.php \Drupal\ajax_example\Form\SubmitDriven::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

modules/ajax_example/src/Form/SubmitDriven.php, line 23

Class

SubmitDriven
Submit a form without a page reload.

Namespace

Drupal\ajax_example\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // This container wil be replaced by AJAX.
  $form['container'] = [
    '#type' => 'container',
    '#attributes' => [
      'id' => 'box-container',
    ],
  ];

  // The box contains some markup that we can change on a submit request.
  $form['container']['box'] = [
    '#type' => 'markup',
    '#markup' => '<h1>Initial markup for box</h1>',
  ];
  $form['submit'] = [
    '#type' => 'submit',
    // The AJAX handler will call our callback, and will replace whatever page
    // element has id box-container.
    '#ajax' => [
      'callback' => '::promptCallback',
      'wrapper' => 'box-container',
    ],
    '#value' => $this
      ->t('Submit'),
  ];
  return $form;
}