You are here

public function BuildDemo::buildForm in Examples for Developers 8

Same name and namespace in other branches
  1. 3.x modules/form_api_example/src/Form/BuildDemo.php \Drupal\form_api_example\Form\BuildDemo::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

form_api_example/src/Form/BuildDemo.php, line 50

Class

BuildDemo
Implements the build demo form controller.

Namespace

Drupal\form_api_example\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['description'] = [
    '#type' => 'item',
    '#markup' => $this
      ->t('Demonstrates how submit, rebuild, form-rebuild and #ajax submit work.'),
  ];

  // Simple checkbox for ajax orders.
  $form['change'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Change Me'),
    '#ajax' => [
      'callback' => '::ajaxSubmit',
      'wrapper' => 'message-wrapper',
    ],
  ];
  $form['actions'] = [
    '#type' => 'actions',
  ];

  // Add a submit button that handles the submission of the form.
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => 'Submit',
  ];

  // Add button handlers.
  $form['actions']['button'] = [
    '#type' => 'button',
    '#value' => 'Rebuild',
  ];
  $form['actions']['rebuild'] = [
    '#type' => 'submit',
    '#value' => 'Submit Rebuild',
    '#submit' => [
      '::rebuildFormSubmit',
    ],
  ];
  $form['actions']['ajaxsubmit'] = [
    '#type' => 'submit',
    '#value' => 'Ajax Submit',
    '#ajax' => [
      'callback' => '::ajaxSubmit',
      'wrapper' => 'message-wrapper',
    ],
  ];
  $form['messages'] = [
    '#type' => 'container',
    '#attributes' => [
      'id' => 'message-wrapper',
    ],
  ];
  return $form;
}