diff --git a/core/lib/Drupal/Core/Form/FormCache.php b/core/lib/Drupal/Core/Form/FormCache.php index c399f1ad07ddffed7c70d8883878e88220734a5c..9eba0426ebf3f916ae4f99790af491eb6a9eec2f 100644 --- a/core/lib/Drupal/Core/Form/FormCache.php +++ b/core/lib/Drupal/Core/Form/FormCache.php @@ -183,8 +183,9 @@ public function setCache($form_build_id, $form, FormStateInterface $form_state) // Ensure that the form build_id embedded in the form structure is the same // as the one passed in as a parameter. This is an additional safety measure - // to prevent legacy code operating directly with form_get_cache and - // form_set_cache from accidentally overwriting immutable form state. + // to prevent legacy code operating directly with + // \Drupal::formBuilder()->getCache() and \Drupal::formBuilder()->setCache() + // from accidentally overwriting immutable form state. if (isset($form['#build_id']) && $form['#build_id'] != $form_build_id) { $this->logger->error('Form build-id mismatch detected while attempting to store a form in the cache.'); return; diff --git a/core/lib/Drupal/Core/Form/FormState.php b/core/lib/Drupal/Core/Form/FormState.php index 09db977d9b9ccc757cbdacaaec709bdd4f22ee7f..5ca9757a87bc5f99f734094d2828f19ceb098195 100644 --- a/core/lib/Drupal/Core/Form/FormState.php +++ b/core/lib/Drupal/Core/Form/FormState.php @@ -50,8 +50,8 @@ class FormState implements FormStateInterface { * for building the form. Each array entry may be the path to a file or * another array containing values for the parameters 'type', 'module' and * 'name' as needed by module_load_include(). The files listed here are - * automatically loaded by form_get_cache(). By default the current menu - * router item's 'file' definition is added, if any. Use + * automatically loaded by \Drupal::formBuilder()->getCache(). By default + * the current menu router item's 'file' definition is added, if any. Use * self::loadInclude() to add include files from a form constructor. * - form_id: Identification of the primary form being constructed and * processed. diff --git a/core/modules/system/src/Controller/FormAjaxController.php b/core/modules/system/src/Controller/FormAjaxController.php index c0a54e642a9df6d78487383d36b9d49e6e3ab93d..81ec059c92ab9057aefc6059c1fcd05a9c42e3e6 100644 --- a/core/modules/system/src/Controller/FormAjaxController.php +++ b/core/modules/system/src/Controller/FormAjaxController.php @@ -116,7 +116,7 @@ protected function getForm(Request $request) { $form_build_id = $request->request->get('form_build_id'); // Get the form from the cache. - $form = form_get_cache($form_build_id, $form_state); + $form = \Drupal::formBuilder()->getCache($form_build_id, $form_state); if (!$form) { // If $form cannot be loaded from the cache, the form_build_id must be // invalid, which means that someone performed a POST request onto @@ -128,9 +128,9 @@ protected function getForm(Request $request) { } // When a page level cache is enabled, the form-build id might have been - // replaced from within form_get_cache. If this is the case, it is also - // necessary to update it in the browser by issuing an appropriate Ajax - // command. + // replaced from within \Drupal::formBuilder()->getCache(). If this is the + // case, it is also necessary to update it in the browser by issuing an + // appropriate Ajax command. $commands = []; if (isset($form['#build_id_old']) && $form['#build_id_old'] != $form['#build_id']) { // If the form build ID has changed, issue an Ajax command to update it. diff --git a/core/modules/system/src/Tests/Form/FormCacheTest.php b/core/modules/system/src/Tests/Form/FormCacheTest.php index 20404d5946036c10dce37ca33bc3ff051f216f01..79bf6809fabc0b0bf8e6a7113c7cdd8f5807edf1 100644 --- a/core/modules/system/src/Tests/Form/FormCacheTest.php +++ b/core/modules/system/src/Tests/Form/FormCacheTest.php @@ -12,7 +12,8 @@ use Drupal\simpletest\DrupalUnitTestBase; /** - * Tests form_set_cache() and form_get_cache(). + * Tests \Drupal::formBuilder()->setCache() and + * \Drupal::formBuilder()->getCache(). * * @group Form */ @@ -57,7 +58,7 @@ protected function setUp() { */ function testCacheToken() { \Drupal::currentUser()->setAccount(new UserSession(array('uid' => 1))); - form_set_cache($this->form_build_id, $this->form, $this->form_state); + \Drupal::formBuilder()->setCache($this->form_build_id, $this->form, $this->form_state); $cached_form_state = new FormState(); $cached_form = form_get_cache($this->form_build_id, $cached_form_state); @@ -70,7 +71,7 @@ function testCacheToken() { // will break the parent site test runner batch.) \Drupal::state()->set('system.private_key', 'invalid'); $cached_form_state = new FormState(); - $cached_form = form_get_cache($this->form_build_id, $cached_form_state); + $cached_form = \Drupal::formBuilder()->getCache($this->form_build_id, $cached_form_state); $this->assertFalse($cached_form, 'No form returned from cache'); $cached_form_state_example = $cached_form_state->get('example'); $this->assertTrue(empty($cached_form_state_example)); @@ -78,7 +79,7 @@ function testCacheToken() { // Test that loading the cache with a different form_id fails. $wrong_form_build_id = $this->randomMachineName(9); $cached_form_state = new FormState(); - $this->assertFalse(form_get_cache($wrong_form_build_id, $cached_form_state), 'No form returned from cache'); + $this->assertFalse(\Drupal::formBuilder()->getCache($wrong_form_build_id, $cached_form_state), 'No form returned from cache'); $cached_form_state_example = $cached_form_state->get('example'); $this->assertTrue(empty($cached_form_state_example), 'Cached form state was not loaded'); } @@ -90,10 +91,10 @@ function testNoCacheToken() { $this->container->set('current_user', new UserSession(array('uid' => 0))); $this->form_state->set('example', $this->randomMachineName()); - form_set_cache($this->form_build_id, $this->form, $this->form_state); + \Drupal::formBuilder()->setCache($this->form_build_id, $this->form, $this->form_state); $cached_form_state = new FormState(); - $cached_form = form_get_cache($this->form_build_id, $cached_form_state); + $cached_form = \Drupal::formBuilder()->getCache($this->form_build_id, $cached_form_state); $this->assertEqual($this->form['#property'], $cached_form['#property']); $this->assertTrue(empty($cached_form['#cache_token']), 'Form has no cache token'); $this->assertEqual($this->form_state->get('example'), $cached_form_state->get('example')); diff --git a/core/modules/system/src/Tests/Form/StorageTest.php b/core/modules/system/src/Tests/Form/StorageTest.php index ca690a6f6787c3f5913274baec4329776952716d..9e99ec3884be4e427100f1c4242980e1f01a890c 100644 --- a/core/modules/system/src/Tests/Form/StorageTest.php +++ b/core/modules/system/src/Tests/Form/StorageTest.php @@ -224,9 +224,10 @@ public function testImmutableFormLegacyProtection() { $this->assertEqual($original['form']['#build_id_old'], $build_id, 'Original build_id was recorded'); $this->assertNotEqual($original['form']['#build_id'], $build_id, 'New build_id was generated'); - // Assert that a watchdog message was logged by form_set_cache. + // Assert that a watchdog message was logged by + // \Drupal::formBuilder()->setCache(). $status = (bool) db_query_range('SELECT 1 FROM {watchdog} WHERE message = :message', 0, 1, [':message' => 'Form build-id mismatch detected while attempting to store a form in the cache.']); - $this->assert($status, 'A watchdog message was logged by form_set_cache'); + $this->assert($status, 'A watchdog message was logged by \Drupal::formBuilder()->setCache'); // Ensure that the form state was not poisoned by the preceding call. $original = $this->drupalGetAJAX('form-test/form-storage-legacy/' . $build_id);