/** * Gets a form submitted via #ajax during an Ajax callback. * * This will load a form from the form cache used during Ajax operations. It * pulls the form info from the request body. * * @param \Symfony\Component\HttpFoundation\Request $request * The current request object. * * @return array * An array containing the $form and $form_state. Use the list() function * to break these apart: * @code * list($form, $form_state, $form_id, $form_build_id) = $this->getForm(); * @endcode * * @throws Symfony\Component\HttpKernel\Exception\HttpExceptionInterface */ protected function getForm(Request $request) { $form_state = \Drupal::formBuilder()->getFormStateDefaults(); $form_build_id = $request->request->get('form_build_id'); // Get the form from the cache. $form = form_get_cache($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 // system/ajax without actually viewing the concerned form in the browser. // This is likely a hacking attempt as it never happens under normal // circumstances. watchdog('ajax', 'Invalid form POST data.', array(), WATCHDOG_WARNING); throw new BadRequestHttpException(); } // Since some of the submit handlers are run, redirects need to be disabled. $form_state['no_redirect'] = TRUE; // When a form is rebuilt after Ajax processing, its #build_id and #action // should not change. // @see \Drupal\Core\Form\FormBuilderInterface::rebuildForm() $form_state['rebuild_info']['copy']['#build_id'] = TRUE; $form_state['rebuild_info']['copy']['#action'] = TRUE; // The form needs to be processed; prepare for that by setting a few internal // variables. $form_state['input'] = $request->request->all(); $form_id = $form['#form_id']; return array($form, $form_state, $form_id, $form_build_id); }
/** * Tests the form cache with a logged-in user. */ function testCacheToken() { \Drupal::currentUser()->setAccount(new UserSession(array('uid' => 1))); \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); $this->assertEqual($this->form['#property'], $cached_form['#property']); $this->assertTrue(!empty($cached_form['#cache_token']), 'Form has a cache token'); $this->assertEqual($this->form_state->get('example'), $cached_form_state->get('example')); // Test that the form cache isn't loaded when the session/token has changed. // Change the private key. (We cannot change the session ID because this // will break the parent site test runner batch.) \Drupal::state()->set('system.private_key', 'invalid'); $cached_form_state = new FormState(); $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)); // 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(\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'); }
/** * Tests the form cache without a logged-in user. */ function testNoCacheToken() { $this->container->set('current_user', new UserSession(array('uid' => 0))); $this->form_state['example'] = $this->randomMachineName(); form_set_cache($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); $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['example'], $cached_form_state['example']); }
/** * (AHAH) retrieve previously cached form */ protected function getCachedForm(&$form_state) { $fbid = $this->getFormBuildId(); return form_get_cache($fbid, $form_state); }
/** * Add domain * * URL: http://guifi.net/guifi/js/add-domain */ function guifi_ahah_add_domain() { $form_state = array('storage' => NULL, 'submitted' => FALSE); $form_build_id = $_POST['form_build_id']; $form = form_get_cache($form_build_id, $form_state); $args = $form['#parameters']; $form_id = array_shift($args); $form_state['post'] = $form['#post'] = $_POST; $form['#programmed'] = $form['#redirect'] = FALSE; drupal_process_form($form_id, $form, $form_state); $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id); $textfields = $form['domain_type_form']; $output = drupal_render($textfields); // Final rendering callback. print drupal_json(array('status' => TRUE, 'data' => $output)); exit; }
/** * This is a re-implementation of drupal_get_form, which returns the form_build_id along with the rendred form */ function _nodereferrer_create_get_form($form_id) { $form_state = array('storage' => NULL, 'submitted' => FALSE); $args = func_get_args(); $cacheable = FALSE; if (isset($_SESSION['batch_form_state'])) { // We've been redirected here after a batch processing : the form has // already been processed, so we grab the post-process $form_state value // and move on to form display. See _batch_finished() function. $form_state = $_SESSION['batch_form_state']; unset($_SESSION['batch_form_state']); } else { // If the incoming $_POST contains a form_build_id, we'll check the // cache for a copy of the form in question. If it's there, we don't // have to rebuild the form to proceed. In addition, if there is stored // form_state data from a previous step, we'll retrieve it so it can // be passed on to the form processing code. if (isset($_POST['form_id']) && $_POST['form_id'] == $form_id && !empty($_POST['form_build_id'])) { $form = form_get_cache($_POST['form_build_id'], $form_state); } // If the previous bit of code didn't result in a populated $form // object, we're hitting the form for the first time and we need // to build it from scratch. if (!isset($form)) { $form_state['post'] = $_POST; // Use a copy of the function's arguments for manipulation $args_temp = $args; $args_temp[0] =& $form_state; array_unshift($args_temp, $form_id); $form = call_user_func_array('drupal_retrieve_form', $args_temp); $form_build_id = 'form-' . md5(mt_rand()); $form['#build_id'] = $form_build_id; drupal_prepare_form($form_id, $form, $form_state); // Store a copy of the unprocessed form for caching and indicate that it // is cacheable if #cache will be set. $original_form = $form; $cacheable = TRUE; unset($form_state['post']); } $form['#post'] = $_POST; // Now that we know we have a form, we'll process it (validating, // submitting, and handling the results returned by its submission // handlers. Submit handlers accumulate data in the form_state by // altering the $form_state variable, which is passed into them by // reference. drupal_process_form($form_id, $form, $form_state); if ($cacheable && !empty($form['#cache'])) { // Caching is done past drupal_process_form so #process callbacks can // set #cache. By not sending the form state, we avoid storing // $form_state['storage']. form_set_cache($form_build_id, $original_form, NULL); } } // Most simple, single-step forms will be finished by this point -- // drupal_process_form() usually redirects to another page (or to // a 'fresh' copy of the form) once processing is complete. If one // of the form's handlers has set $form_state['redirect'] to FALSE, // the form will simply be re-rendered with the values still in its // fields. // // If $form_state['storage'] or $form_state['rebuild'] have been // set by any submit or validate handlers, however, we know that // we're in a complex multi-part process of some sort and the form's // workflow is NOT complete. We need to construct a fresh copy of // the form, passing in the latest $form_state in addition to any // other variables passed into drupal_get_form(). if (!empty($form_state['rebuild']) || !empty($form_state['storage'])) { $form = drupal_rebuild_form($form_id, $form_state, $args); } // If we haven't redirected to a new location by now, we want to // render whatever form array is currently in hand. return array('form_build_id' => $form['#build_id'], 'form' => drupal_render_form($form_id, $form)); }