function BuildDemo::buildForm

Same name in other branches
  1. 4.0.x modules/form_api_example/src/Form/BuildDemo.php \Drupal\form_api_example\Form\BuildDemo::buildForm()

Overrides FormInterface::buildForm

File

modules/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;
}