summaryrefslogtreecommitdiffstats
path: root/core/modules/system/tests/modules/form_test/src/Form/FormTestStorageForm.php
blob: f81658d537d79d9f129849f8f016f499c267c4d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php

namespace Drupal\form_test\Form;

use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * A multistep form for testing the form storage.
 *
 * It uses two steps for editing a virtual "thing". Any changes to it are saved
 * in the form storage and have to be present during any step. By setting the
 * request parameter "cache" the form can be tested with caching enabled, as
 * it would be the case, if the form would contain some #ajax callbacks.
 *
 * @internal
 */
class FormTestStorageForm extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'form_test_storage_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    if ($form_state->isRebuilding()) {
      $form_state->setUserInput([]);
    }
    // Initialize
    $storage = $form_state->getStorage();
    if (empty($storage)) {
      $user_input = $form_state->getUserInput();
      if (empty($user_input)) {
        $_SESSION['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.
    $_SESSION['constructions']++;
    drupal_set_message("Form constructions: " . $_SESSION['constructions']);

    $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',
      // Rebuilds the form without keeping the values.
    ];
    $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;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    if ($this->getRequest()->get('cache')) {
      // Manually activate caching, so we can test that the storage keeps working
      // when it's enabled.
      $form_state->setCached();
    }
  }

  /**
   * Form element validation handler for 'value' element.
   *
   * Tests updating of cached form storage during validation.
   */
  public function elementValidateValueCached($element, FormStateInterface $form_state) {
    // If caching is enabled and we receive a certain value, change the storage.
    // This presumes that another submitted form value triggers a validation error
    // elsewhere in the form. Form API should still update the cached form storage
    // though.
    if ($this->getRequest()->get('cache') && $form_state->getValue('value') == 'change_title') {
      $form_state->set(['thing', 'changed'], TRUE);
    }
  }

  /**
   * {@inheritdoc}
   */
  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();
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    drupal_set_message("Title: " . Html::escape($form_state->getValue('title')));
    drupal_set_message("Form constructions: " . $_SESSION['constructions']);
    if ($form_state->has(['thing', 'changed'])) {
      drupal_set_message("The thing has been changed.");
    }
    $form_state->setRedirect('<front>');
  }

}