Problem/Motivation

The class responsible for building forms currently talks directly to the KV stores for form/form_state cache.
None of this is really central to building of forms, and should be handled by a separate object.

Proposed resolution

Move getCache/setCache to a new FormCacheInterface

FormBuilder will implement this and pass calls through

Remaining tasks

User interface changes

API changes

N/A

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tim.plunkett’s picture

Status: Active » Needs review
FileSize
34.01 KB
dawehner’s picture

  1. +++ b/core/core.services.yml
    @@ -160,13 +160,16 @@ services:
       form_validator:
         class: Drupal\Core\Form\FormValidator
         arguments: ['@request_stack', '@string_translation', '@csrf_token', '@logger.channel.form']
       form_submitter:
         class: Drupal\Core\Form\FormSubmitter
         arguments: ['@request_stack', '@url_generator']
    +  form_cache:
    +    class: Drupal\Core\Form\FormCache
    +    arguments: ['@keyvalue.expirable', '@module_handler', '@?csrf_token']
    

    Did you considered to mark them as private?

  2. +++ b/core/lib/Drupal/Core/Form/FormBuilder.php
    @@ -108,16 +99,21 @@ class FormBuilder implements FormBuilderInterface, FormValidatorInterface, FormS
       /**
    +   * @var \Drupal\Core\Form\FormCacheInterface
    +   */
    +  protected $formCache;
    +
    

    Let's put some documentation on there.

  3. +++ b/core/lib/Drupal/Core/Form/FormCache.php
    @@ -0,0 +1,152 @@
    +      if ($this->currentUser()->isAuthenticated()) {
    ...
    +  /**
    +   * 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;
    +  }
    

    Can't you instead provide an optional constructor dependency? How does that work, to not fail? This code assumes that the current user always exists.

tim.plunkett’s picture

FileSize
33.46 KB
6.3 KB

I had this as private locally, reverted it at the last minute :)
I copied currentUser() from FormBuilder, but you're right, we don't need to do that.

jibran’s picture

It is simple re-factoring so +1 but I must say new test coverage is amazing. Just minor issues need some clarification other then that RTBC.

  1. +++ b/core/core.services.yml
    @@ -160,13 +160,17 @@ services:
    +    public: false  # Private to form_builder
    

    Please move this above the line like all other inline comments.

  2. +++ b/core/tests/Drupal/Tests/Core/Form/FormCacheTest.php
    @@ -0,0 +1,402 @@
    +    $this->keyValueExpirableFactory = $this->getMockBuilder('Drupal\Core\KeyValueStore\KeyValueExpirableFactory')
    +      ->disableOriginalConstructor()
    +      ->getMock();
    

    Why not use simple getMock()?

tim.plunkett’s picture

FileSize
33.42 KB
1.29 KB

1) This is the same comment format as the only other public: false, and
2) Because that is only valid for interfaces,... which we didn't have when the FormBuilder tests went in but we do have now (as of #2160495: Add a KeyValueExpirableFactoryInterface).

jibran’s picture

Status: Needs review » Reviewed & tested by the community

Thank you for explaining that.

  • webchick committed 2191f86 on 8.0.x
    Issue #2328777 by tim.plunkett: Refactor FAPI getCache()/setCache() into...
webchick’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed to 8.x. Thanks!

tim.plunkett’s picture

Assigned: tim.plunkett » Unassigned

Thanks!

Berdir’s picture

Getting those errors now when running unit tests:

There were 12 errors:

1) Drupal\Tests\Core\Form\FormCacheTest::testGetCacheValidToken
serialize(): __sleep should return an array only containing the names of instance-variables to serialize
PHP 5.5.9-1ubuntu4.3 (cli) (built: Jul  7 2014 16:36:58) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies
    with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans
sun’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.