Same filename and directory in other branches
- 8.9.x core/modules/system/tests/modules/form_test/src/Form/FormTestStorageForm.php
- 9 core/modules/system/tests/modules/form_test/src/Form/FormTestStorageForm.php
Namespace
Drupal\form_test\Form
File
core/modules/system/tests/modules/form_test/src/Form/FormTestStorageForm.php
View source
<?php
namespace Drupal\form_test\Form;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
class FormTestStorageForm extends FormBase {
public function getFormId() {
return 'form_test_storage_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
if ($form_state
->isRebuilding()) {
$form_state
->setUserInput([]);
}
$storage = $form_state
->getStorage();
$session = $this
->getRequest()
->getSession();
if (empty($storage)) {
$user_input = $form_state
->getUserInput();
if (empty($user_input)) {
$session
->set('constructions', 0);
}
$storage = [
'thing' => [
'title' => 'none',
'value' => '',
],
];
$form_state
->setStorage($storage);
}
$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',
];
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;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if ($this
->getRequest()
->get('cache')) {
$form_state
->setCached();
}
}
public function elementValidateValueCached($element, FormStateInterface $form_state) {
if ($this
->getRequest()
->get('cache') && $form_state
->getValue('value') == 'change_title') {
$form_state
->set([
'thing',
'changed',
], TRUE);
}
}
public function continueSubmitForm(array &$form, FormStateInterface $form_state) {
$form_state
->set([
'thing',
'title',
], $form_state
->getValue('title'));
$form_state
->set([
'thing',
'value',
], $form_state
->getValue('value'));
$form_state
->setRebuild();
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this
->messenger()
->addStatus("Title: " . Html::escape($form_state
->getValue('title')));
$this
->messenger()
->addStatus("Form constructions: " . $this
->getRequest()
->getSession()
->get('constructions'));
if ($form_state
->has([
'thing',
'changed',
])) {
$this
->messenger()
->addStatus("The thing has been changed.");
}
$form_state
->setRedirect('<front>');
}
}
Classes