1 form.inc form_get_cache($form_build_id, &$form_state)

Fetches a form from the cache.

Return value

array|NULL: The form array retrieved from cache if found. NULL otherwise.

Related topics

File

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

Code

function form_get_cache($form_build_id, &$form_state) {
  if ($form = tempstore_get('form', $form_build_id)) {

    global $user;
    if ((isset($form['#cache_token']) && backdrop_valid_token($form['#cache_token'])) || (!isset($form['#cache_token']) && !$user->uid)) {
      if ($cached_form_state = tempstore_get('form_state', $form_build_id)) {
        // Re-populate $form_state for subsequent rebuilds.
        $form_state = $cached_form_state + $form_state;

        // If the original form is contained in include files, load the files.
        // @see form_load_include()
        $form_state['build_info'] += array('files' => array());
        foreach ($form_state['build_info']['files'] as $file) {
          if (is_array($file)) {
            $file += array('type' => 'inc', 'name' => $file['module']);
            module_load_include($file['type'], $file['module'], $file['name']);
          }
          elseif (file_exists($file)) {
            require_once BACKDROP_ROOT . '/' . $file;
          }
        }
      }
      // Generate a new #build_id if the cached form was rendered on a cacheable
      // page.
      if (!empty($form_state['build_info']['immutable'])) {
        $form['#build_id_old'] = $form['#build_id'];
        $form['#build_id'] = 'form-' . backdrop_random_key();
        $form['form_build_id']['#value'] = $form['#build_id'];
        $form['form_build_id']['#id'] = $form['#build_id'];
        unset($form_state['build_info']['immutable']);
      }
      return $form;
    }
  }

  return NULL;
}