You are here

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

Same name and namespace in other branches
  1. 8 form_api_example/src/Form/ModalForm.php \Drupal\form_api_example\Form\ModalForm::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/form_api_example/src/Form/ModalForm.php, line 60

Class

ModalForm
Implements the ModalForm form controller.

Namespace

Drupal\form_api_example\Form

Code

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

  // Add the core AJAX library.
  $form['#attached']['library'][] = 'core/drupal.ajax';
  $form['description'] = [
    '#type' => 'item',
    '#markup' => $this
      ->t('This example demonstrates a form that can work as a normal multi-request form, or as a modal dialog using AJAX.'),
  ];

  // Add a link to show this form in a modal dialog if we're not already in
  // one.
  if ($nojs == 'nojs') {
    $form['use_ajax_container'] = [
      '#type' => 'details',
      '#open' => TRUE,
    ];
    $form['use_ajax_container']['description'] = [
      '#type' => 'item',
      '#markup' => $this
        ->t('In order to show a modal dialog by clicking on a link, that link has to have class <code>use-ajax</code> and <code>data-dialog-type="modal"</code>. This link has those attributes.'),
    ];
    $form['use_ajax_container']['use_ajax'] = [
      '#type' => 'link',
      '#title' => $this
        ->t('See this form as a modal.'),
      '#url' => Url::fromRoute('form_api_example.modal_form', [
        'nojs' => 'ajax',
      ]),
      '#attributes' => [
        'class' => [
          'use-ajax',
        ],
        'data-dialog-type' => 'modal',
        'data-dialog-options' => json_encode(static::getDataDialogOptions()),
        // Add this id so that we can test this form.
        'id' => 'ajax-example-modal-link',
      ],
    ];
  }

  // This element is responsible for displaying form errors in the AJAX
  // dialog.
  if ($nojs == 'ajax') {
    $form['status_messages'] = [
      '#type' => 'status_messages',
      '#weight' => -999,
    ];
  }
  $form['title'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Title'),
    '#required' => TRUE,
  ];

  // Group submit handlers in an actions element with a key of "actions" so
  // that it gets styled correctly, and so that other modules may add actions
  // to the form.
  $form['actions'] = [
    '#type' => 'actions',
  ];

  // Add a submit button that handles the submission of the form.
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Submit'),
    '#ajax' => [
      'callback' => '::ajaxSubmitForm',
      'event' => 'click',
    ],
  ];

  // Set the form to not use AJAX if we're on a nojs path. When this form is
  // within the modal dialog, Drupal will make sure we're using an AJAX path
  // instead of a nojs one.
  if ($nojs == 'nojs') {
    unset($form['actions']['submit']['#ajax']);
  }
  return $form;
}