Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Core/Form/FormCache.php \Drupal\Core\Form\FormCache::setCache()
  2. 9 core/lib/Drupal/Core/Form/FormCache.php \Drupal\Core\Form\FormCache::setCache()

File

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

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) {

  // Cache forms for 6 hours by default.
  $expire = Settings::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
  // \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);
  }
}