You are here

function form_set_cache in Drupal 7

Same name and namespace in other branches
  1. 6 includes/form.inc \form_set_cache()

Stores a form in the cache.

Related topics

5 calls to form_set_cache()
drupal_process_form in includes/form.inc
Processes a form submission.
drupal_rebuild_form in includes/form.inc
Constructs a new $form from the information in $form_state.
FormsFormCacheTestCase::testCacheForm in modules/simpletest/tests/form.test
Tests storing and retrieving the form from cache.
FormsFormCacheTestCase::testCacheFormCustomExpiration in modules/simpletest/tests/form.test
Tests changing form_cache_expiration.
form_test_storage_legacy_handler in modules/simpletest/tests/form_test.module
Emulate legacy AHAH-style ajax callback.

File

includes/form.inc, line 557
Functions for form and batch generation and processing.

Code

function form_set_cache($form_build_id, $form, $form_state) {

  // The default cache_form expiration is 6 hours. On busy sites, the cache_form
  // table can become very large. A shorter cache lifetime can help to keep the
  // table's size under control.
  $expire = variable_get('form_cache_expiration', 21600);

  // 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.
  if ($form['#build_id'] != $form_build_id) {
    watchdog('form', 'Form build-id mismatch detected while attempting to store a form in the cache.', array(), WATCHDOG_ERROR);
    return;
  }

  // Cache form structure.
  if (isset($form)) {
    if ($GLOBALS['user']->uid) {
      $form['#cache_token'] = drupal_get_token();
    }
    unset($form['#build_id_old']);
    cache_set('form_' . $form_build_id, $form, 'cache_form', REQUEST_TIME + $expire);
  }

  // Cache form state.
  if (variable_get('cache', 0) && drupal_page_is_cacheable()) {
    $form_state['build_info']['immutable'] = TRUE;
  }
  if ($data = array_diff_key($form_state, array_flip(form_state_keys_no_cache()))) {
    cache_set('form_state_' . $form_build_id, $data, 'cache_form', REQUEST_TIME + $expire);
  }
}