You are here

public function FormTestStorageForm::buildForm in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/system/tests/modules/form_test/src/Form/FormTestStorageForm.php \Drupal\form_test\Form\FormTestStorageForm::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/form_test/src/Form/FormTestStorageForm.php, line 31

Class

FormTestStorageForm
A multistep form for testing the form storage.

Namespace

Drupal\form_test\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  if ($form_state
    ->isRebuilding()) {
    $form_state
      ->setUserInput([]);
  }

  // Initialize
  $storage = $form_state
    ->getStorage();
  $session = $this
    ->getRequest()
    ->getSession();
  if (empty($storage)) {
    $user_input = $form_state
      ->getUserInput();
    if (empty($user_input)) {
      $session
        ->set('constructions', 0);
    }

    // Put the initial thing into the storage
    $storage = [
      'thing' => [
        'title' => 'none',
        'value' => '',
      ],
    ];
    $form_state
      ->setStorage($storage);
  }

  // Count how often the form is constructed.
  $counter = $session
    ->get('constructions');
  $session
    ->set('constructions', ++$counter);
  $this
    ->messenger()
    ->addStatus("Form constructions: " . $counter);
  $form['title'] = [
    '#type' => 'textfield',
    '#title' => 'Title',
    '#default_value' => $storage['thing']['title'],
    '#required' => TRUE,
  ];
  $form['value'] = [
    '#type' => 'textfield',
    '#title' => 'Value',
    '#default_value' => $storage['thing']['value'],
    '#element_validate' => [
      '::elementValidateValueCached',
    ],
  ];
  $form['continue_button'] = [
    '#type' => 'button',
    '#value' => 'Reset',
  ];
  $form['continue_submit'] = [
    '#type' => 'submit',
    '#value' => 'Continue submit',
    '#submit' => [
      '::continueSubmitForm',
    ],
  ];
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => 'Save',
  ];

  // @todo Remove this in https://www.drupal.org/node/2524408, because form
  //   cache immutability is no longer necessary, because we no longer cache
  //   forms during safe HTTP methods. In the meantime, because
  //   Drupal\system\Tests\Form still has test coverage for a poisoned form
  //   cache following a GET request, trick $form_state into caching the form
  //   to keep that test working until we either remove it or change it in
  //   that issue.
  if ($this
    ->getRequest()
    ->get('immutable')) {
    $form_state
      ->addBuildInfo('immutable', TRUE);
    if ($this
      ->getRequest()
      ->get('cache') && $this
      ->getRequest()
      ->isMethodCacheable()) {
      $form_state
        ->setRequestMethod('FAKE');
      $form_state
        ->setCached();
    }
  }
  return $form;
}