Same filename and directory in other branches
- 8.9.x core/lib/Drupal/Core/Form/FormCache.php
- 9 core/lib/Drupal/Core/Form/FormCache.php
Namespace
Drupal\Core\Form
File
core/lib/Drupal/Core/Form/FormCache.php
View source
<?php
namespace Drupal\Core\Form;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Access\CsrfTokenGenerator;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface;
use Drupal\Core\PageCache\RequestPolicyInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Site\Settings;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class FormCache implements FormCacheInterface {
protected $keyValueExpirableFactory;
protected $csrfToken;
protected $currentUser;
protected $moduleHandler;
protected $logger;
protected $configFactory;
protected $requestStack;
protected $requestPolicy;
protected $root;
public function __construct($root, KeyValueExpirableFactoryInterface $key_value_expirable_factory, ModuleHandlerInterface $module_handler, AccountInterface $current_user, CsrfTokenGenerator $csrf_token, LoggerInterface $logger, RequestStack $request_stack, RequestPolicyInterface $request_policy) {
$this->root = $root;
$this->keyValueExpirableFactory = $key_value_expirable_factory;
$this->moduleHandler = $module_handler;
$this->currentUser = $current_user;
$this->logger = $logger;
$this->csrfToken = $csrf_token;
$this->requestStack = $request_stack;
$this->requestPolicy = $request_policy;
}
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);
$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;
}
}
}
protected function loadCachedFormState($form_build_id, FormStateInterface $form_state) {
if ($stored_form_state = $this->keyValueExpirableFactory
->get('form_state')
->get($form_build_id)) {
$form_state
->setFormState($stored_form_state);
$build_info = $form_state
->getBuildInfo();
$build_info += [
'files' => [],
];
foreach ($build_info['files'] as $file) {
if (is_array($file)) {
$file += [
'type' => 'inc',
'name' => $file['module'],
];
$this->moduleHandler
->loadInclude($file['module'], $file['type'], $file['name']);
}
elseif (file_exists($file)) {
require_once $this->root . '/' . $file;
}
}
}
}
public function setCache($form_build_id, $form, FormStateInterface $form_state) {
$expire = Settings::get('form_cache_expiration', 21600);
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;
}
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);
}
}
public function deleteCache($form_build_id) {
$this->keyValueExpirableFactory
->get('form')
->delete($form_build_id);
$this->keyValueExpirableFactory
->get('form_state')
->delete($form_build_id);
}
}
Classes
Name |
Description |
FormCache |
Encapsulates the caching of a form and its form state. |