W3cubDocs

/Drupal 8

public function FormCache::getCache

public FormCache::getCache($form_build_id, FormStateInterface $form_state)

Fetches a form from the cache.

Parameters

string $form_build_id: The unique form build ID.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormCacheInterface::getCache

File

core/lib/Drupal/Core/Form/FormCache.php, line 119

Class

FormCache
Encapsulates the caching of a form and its form state.

Namespace

Drupal\Core\Form

Code

public function getCache($form_build_id, FormStateInterface $form_state) {
  if ($form = $this->keyValueExpirableFactory->get('form')->get($form_build_id)) {
    if ((isset($form['#cache_token']) && $this->csrfToken->validate($form['#cache_token'])) || (!isset($form['#cache_token']) && $this->currentUser->isAnonymous())) {
      $this->loadCachedFormState($form_build_id, $form_state);

      // Generate a new #build_id if the cached form was rendered on a
      // cacheable page.
      $build_info = $form_state->getBuildInfo();
      if (!empty($build_info['immutable'])) {
        $form['#build_id_old'] = $form['#build_id'];
        $form['#build_id'] = 'form-' . Crypt::randomBytesBase64();
        $form['form_build_id']['#value'] = $form['#build_id'];
        $form['form_build_id']['#id'] = $form['#build_id'];
        unset($build_info['immutable']);
        $form_state->setBuildInfo($build_info);
      }
      return $form;
    }
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Form!FormCache.php/function/FormCache::getCache/8.1.x