/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { if ($form_state->isRebuilding()) { $form_state->setUserInput(array()); } // 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'] = array('#type' => 'textfield', '#title' => 'Title', '#default_value' => $storage['thing']['title'], '#required' => TRUE); $form['value'] = array('#type' => 'textfield', '#title' => 'Value', '#default_value' => $storage['thing']['value'], '#element_validate' => array('::elementValidateValueCached')); $form['continue_button'] = array('#type' => 'button', '#value' => 'Reset'); $form['continue_submit'] = array('#type' => 'submit', '#value' => 'Continue submit', '#submit' => array('::continueSubmitForm')); $form['submit'] = array('#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()->isMethodSafe()) { $form_state->setRequestMethod('FAKE'); $form_state->setCached(); } } return $form; }
/** * {@inheritdoc} */ public function setRequestMethod($method) { $this->mainFormState->setRequestMethod($method); return $this; }
/** * @covers ::setRequestMethod * * @dataProvider providerSetRequestMethod * * @param bool $method */ public function testSetRequestMethod($method) { $this->decoratedFormState->setRequestMethod($method)->shouldBeCalled(); $this->assertSame($this->formStateDecoratorBase, $this->formStateDecoratorBase->setRequestMethod($method)); }