diff --git a/core/core.services.yml b/core/core.services.yml index c877f54..c068880 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -182,7 +182,7 @@ services: arguments: ['@request_stack', '@url_generator'] form_cache: class: Drupal\Core\Form\FormCache - arguments: ['@keyvalue.expirable', '@module_handler', '@current_user', '@csrf_token', '@logger.channel.form', '@config.factory'] + arguments: ['@keyvalue.expirable', '@module_handler', '@current_user', '@csrf_token', '@logger.channel.form', '@config.factory', '@request_stack', '@page_cache_request_policy'] public: false # Private to form_builder keyvalue: class: Drupal\Core\KeyValueStore\KeyValueFactory diff --git a/core/lib/Drupal/Core/Form/FormCache.php b/core/lib/Drupal/Core/Form/FormCache.php index 4cc3f40..feb66d4 100644 --- a/core/lib/Drupal/Core/Form/FormCache.php +++ b/core/lib/Drupal/Core/Form/FormCache.php @@ -16,6 +16,7 @@ use Drupal\Core\PageCache\RequestPolicyInterface; use Drupal\Core\Session\AccountInterface; use Psr\Log\LoggerInterface; +use Symfony\Component\HttpFoundation\RequestStack; /** * Encapsulates the caching of a form and its form state. @@ -67,6 +68,20 @@ class FormCache implements FormCacheInterface { protected $configFactory; /** + * The request stack. + * + * @var \Symfony\Component\HttpFoundation\RequestStack + */ + protected $request; + + /** + * A policy rule determining the cacheability of a request. + * + * @var \Drupal\Core\PageCache\RequestPolicyInterface + */ + protected $requestPolicy; + + /** * Constructs a new FormCache. * * @param \Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface $key_value_expirable_factory @@ -82,14 +97,20 @@ class FormCache implements FormCacheInterface { * A logger instance. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The configuration factory. + * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack + * The request stack. + * @param \Drupal\Core\PageCache\RequestPolicyInterface $request_policy + * A policy rule determining the cacheability of a request. */ - public function __construct(KeyValueExpirableFactoryInterface $key_value_expirable_factory, ModuleHandlerInterface $module_handler, AccountInterface $current_user, CsrfTokenGenerator $csrf_token, LoggerInterface $logger, ConfigFactoryInterface $config_factory) { + public function __construct(KeyValueExpirableFactoryInterface $key_value_expirable_factory, ModuleHandlerInterface $module_handler, AccountInterface $current_user, CsrfTokenGenerator $csrf_token, LoggerInterface $logger, ConfigFactoryInterface $config_factory, RequestStack $request_stack, RequestPolicyInterface $request_policy) { $this->keyValueExpirableFactory = $key_value_expirable_factory; $this->moduleHandler = $module_handler; $this->currentUser = $current_user; $this->logger = $logger; $this->configFactory = $config_factory; $this->csrfToken = $csrf_token; + $this->request = $request_stack; + $this->requestPolicy = $request_policy; } /** @@ -197,8 +218,7 @@ public function setCache($form_build_id, $form, FormStateInterface $form_state) * Checks if the page is cacheable. */ protected function isPageCacheable($allow_caching = NULL) { - $request_policy = \Drupal::service('page_cache_request_policy'); - return ($request_policy->check(\Drupal::request()) === RequestPolicyInterface::ALLOW); + return ($this->request_policy->check($this->request) === RequestPolicyInterface::ALLOW); } }