public function FormCache::setCache

public FormCache::setCache($form_build_id, $form, FormStateInterface $form_state)

Stores a form in the cache.

Parameters

string $form_build_id: The unique form build ID.

array $form: The form to cache.

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

Overrides FormCacheInterface::setCache

File

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

Class

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

Namespace

Drupal\Core\Form

Code

public function setCache($form_build_id, $form, FormStateInterface $form_state) {
  // 6 hours cache life time for forms should be plenty.
  $expire = 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
  // \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;
  }

  // Cache form structure.
  if (isset($form)) {
    if ($this->currentUser->isAuthenticated()) {
      $form['#cache_token'] = $this->csrfToken->get();
    }
    unset($form['#build_id_old']);
    $this->keyValueExpirableFactory->get('form')->setWithExpire($form_build_id, $form, $expire);
  }

  if ($data = $form_state->getCacheableArray()) {
    $this->keyValueExpirableFactory->get('form_state')->setWithExpire($form_build_id, $data, $expire);
  }
}

© 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::setCache/8.1.x