diff --git a/core/core.services.yml b/core/core.services.yml index 72a54ac..b68dd90 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -169,7 +169,8 @@ services: arguments: ['@request_stack', '@url_generator'] form_cache: class: Drupal\Core\Form\FormCache - arguments: ['@keyvalue.expirable', '@module_handler', '@?csrf_token'] + arguments: ['@keyvalue.expirable', '@module_handler', '@current_user', '@csrf_token'] + public: false # Private to form_builder keyvalue: class: Drupal\Core\KeyValueStore\KeyValueFactory arguments: ['@service_container', '%factory.keyvalue%'] diff --git a/core/includes/form.inc b/core/includes/form.inc index 3fa3056..4938933 100644 --- a/core/includes/form.inc +++ b/core/includes/form.inc @@ -23,24 +23,24 @@ * Fetches a form from the cache. * * @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0. - * Use \Drupal::service('form_cache')->getCache(). + * Use \Drupal::formBuilder()->getCache(). * * @see \Drupal\Core\Form\FormCacheInterface::getCache(). */ function form_get_cache($form_build_id, FormStateInterface $form_state) { - return \Drupal::service('form_cache')->getCache($form_build_id, $form_state); + return \Drupal::formBuilder()->getCache($form_build_id, $form_state); } /** * Stores a form in the cache. * * @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0. - * Use \Drupal::service('form_cache')->setCache(). + * Use \Drupal::formBuilder()->setCache(). * * @see \Drupal\Core\Form\FormCacheInterface::setCache(). */ function form_set_cache($form_build_id, $form, FormStateInterface $form_state) { - \Drupal::service('form_cache')->setCache($form_build_id, $form, $form_state); + \Drupal::formBuilder()->setCache($form_build_id, $form, $form_state); } /** diff --git a/core/lib/Drupal/Core/Form/FormBuilder.php b/core/lib/Drupal/Core/Form/FormBuilder.php index 40d15e8..429aceb 100644 --- a/core/lib/Drupal/Core/Form/FormBuilder.php +++ b/core/lib/Drupal/Core/Form/FormBuilder.php @@ -99,6 +99,8 @@ class FormBuilder implements FormBuilderInterface, FormValidatorInterface, FormS protected $formSubmitter; /** + * The form cache. + * * @var \Drupal\Core\Form\FormCacheInterface */ protected $formCache; diff --git a/core/lib/Drupal/Core/Form/FormCache.php b/core/lib/Drupal/Core/Form/FormCache.php index adf9865..d627828 100644 --- a/core/lib/Drupal/Core/Form/FormCache.php +++ b/core/lib/Drupal/Core/Form/FormCache.php @@ -11,6 +11,7 @@ use Drupal\Core\Access\CsrfTokenGenerator; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface; +use Drupal\Core\Session\AccountInterface; /** * Encapsulates the caching of a form and its form state. @@ -55,12 +56,15 @@ class FormCache implements FormCacheInterface { * stores for the form cache and form state cache. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler. + * @param \Drupal\Core\Session\AccountInterface $current_user + * The current user. * @param \Drupal\Core\Access\CsrfTokenGenerator $csrf_token * The CSRF token generator. */ - public function __construct(KeyValueExpirableFactoryInterface $key_value_expirable_factory, ModuleHandlerInterface $module_handler, CsrfTokenGenerator $csrf_token = NULL) { + public function __construct(KeyValueExpirableFactoryInterface $key_value_expirable_factory, ModuleHandlerInterface $module_handler, AccountInterface $current_user, CsrfTokenGenerator $csrf_token = NULL) { $this->keyValueExpirableFactory = $key_value_expirable_factory; $this->moduleHandler = $module_handler; + $this->currentUser = $current_user; $this->csrfToken = $csrf_token; } @@ -69,7 +73,7 @@ public function __construct(KeyValueExpirableFactoryInterface $key_value_expirab */ 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())) { + 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); return $form; } @@ -120,7 +124,7 @@ public function setCache($form_build_id, $form, FormStateInterface $form_state) // Cache form structure. if (isset($form)) { - if ($this->currentUser()->isAuthenticated()) { + if ($this->currentUser->isAuthenticated()) { $form['#cache_token'] = $this->csrfToken->get(); } $this->keyValueExpirableFactory->get('form')->setWithExpire($form_build_id, $form, $expire); @@ -137,16 +141,4 @@ public function setCache($form_build_id, $form, FormStateInterface $form_state) } } - /** - * Gets the current active user. - * - * @return \Drupal\Core\Session\AccountInterface - */ - protected function currentUser() { - if (!$this->currentUser && \Drupal::hasService('current_user')) { - $this->currentUser = \Drupal::currentUser(); - } - return $this->currentUser; - } - } diff --git a/core/tests/Drupal/Tests/Core/Form/FormCacheTest.php b/core/tests/Drupal/Tests/Core/Form/FormCacheTest.php index c132ea6..a9f28c6 100644 --- a/core/tests/Drupal/Tests/Core/Form/FormCacheTest.php +++ b/core/tests/Drupal/Tests/Core/Form/FormCacheTest.php @@ -93,8 +93,7 @@ protected function setUp() { ->disableOriginalConstructor() ->getMock(); $this->account = $this->getMock('Drupal\Core\Session\AccountInterface'); - $this->formCache = new TestFormCache($this->keyValueExpirableFactory, $this->moduleHandler, $this->csrfToken); - $this->formCache->setCurrentUser($this->account); + $this->formCache = new FormCache($this->keyValueExpirableFactory, $this->moduleHandler, $this->account, $this->csrfToken); } /** @@ -401,14 +400,3 @@ public function testSetCacheWithSafeStrings() { } } - -class TestFormCache extends FormCache { - - /** - * @param \Drupal\Core\Session\AccountInterface $account - */ - public function setCurrentUser(AccountInterface $account) { - $this->currentUser = $account; - } - -}