diff --git a/core/core.services.yml b/core/core.services.yml index 8527b27..a16b7ae 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -147,7 +147,7 @@ services: arguments: [default] form_builder: class: Drupal\Core\Form\FormBuilder - arguments: ['@form_validator', '@form_submitter', '@module_handler', '@keyvalue.expirable', '@event_dispatcher', '@request_stack', '@class_resolver', '@?csrf_token', '@?http_kernel'] + arguments: ['@form_validator', '@form_submitter', '@module_handler', '@keyvalue.expirable', '@event_dispatcher', '@request_stack', '@class_resolver', '@logger.factory', '@config.factory', '@?csrf_token', '@?http_kernel'] form_validator: class: Drupal\Core\Form\FormValidator arguments: ['@request_stack', '@string_translation', '@csrf_token'] diff --git a/core/lib/Drupal/Core/Form/FormBuilder.php b/core/lib/Drupal/Core/Form/FormBuilder.php index 9051f36..6d549ad 100644 --- a/core/lib/Drupal/Core/Form/FormBuilder.php +++ b/core/lib/Drupal/Core/Form/FormBuilder.php @@ -13,9 +13,11 @@ use Drupal\Component\Utility\String; use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Access\CsrfTokenGenerator; +use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\DependencyInjection\ClassResolverInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface; +use Drupal\Core\Logger\LoggerChannelFactory; use Drupal\Core\Render\Element; use Drupal\Core\Site\Settings; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -100,6 +102,16 @@ class FormBuilder implements FormBuilderInterface, FormValidatorInterface, FormS protected $formSubmitter; /** + * @var \Drupal\Core\Logger\LoggerChannelFactory; + */ + protected $logger; + + /** + * @var \Drupal\Core\Config\ConfigFactoryInterface + */ + protected $configFactory; + + /** * Constructs a new FormBuilder. * * @param \Drupal\Core\Form\FormValidatorInterface $form_validator @@ -116,12 +128,16 @@ class FormBuilder implements FormBuilderInterface, FormValidatorInterface, FormS * The request stack. * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver * The class resolver. + * @param \Drupal\Core\Logger\LoggerChannelFactory $logger + * The logger channel factory. + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * The config factory. * @param \Drupal\Core\Access\CsrfTokenGenerator $csrf_token * The CSRF token generator. * @param \Drupal\Core\HttpKernel $http_kernel * The HTTP kernel. */ - public function __construct(FormValidatorInterface $form_validator, FormSubmitterInterface $form_submitter, ModuleHandlerInterface $module_handler, KeyValueExpirableFactoryInterface $key_value_expirable_factory, EventDispatcherInterface $event_dispatcher, RequestStack $request_stack, ClassResolverInterface $class_resolver, CsrfTokenGenerator $csrf_token = NULL, HttpKernel $http_kernel = NULL) { + public function __construct(FormValidatorInterface $form_validator, FormSubmitterInterface $form_submitter, ModuleHandlerInterface $module_handler, KeyValueExpirableFactoryInterface $key_value_expirable_factory, EventDispatcherInterface $event_dispatcher, RequestStack $request_stack, ClassResolverInterface $class_resolver, LoggerChannelFactory $logger, ConfigFactoryInterface $config_factory, CsrfTokenGenerator $csrf_token = NULL, HttpKernel $http_kernel = NULL) { $this->formValidator = $form_validator; $this->formSubmitter = $form_submitter; $this->moduleHandler = $module_handler; @@ -129,6 +145,8 @@ public function __construct(FormValidatorInterface $form_validator, FormSubmitte $this->eventDispatcher = $event_dispatcher; $this->requestStack = $request_stack; $this->classResolver = $class_resolver; + $this->logger = $logger; + $this->configFactory = $config_factory; $this->csrfToken = $csrf_token; $this->httpKernel = $http_kernel; } @@ -373,7 +391,7 @@ public function setCache($form_build_id, $form, FormStateInterface $form_state) // prevent legacy code operating directly with form_get_cache and // form_set_cache from accidentally overwriting immutable form state. if ($form['#build_id'] != $form_build_id) { - watchdog('form', 'Form build-id mismatch detected while attempting to store a form in the cache.', array(), WATCHDOG_ERROR); + $this->logger->get('form')->log(WATCHDOG_ERROR, 'Form build-id mismatch detected while attempting to store a form in the cache.', array()); return; } @@ -387,7 +405,7 @@ public function setCache($form_build_id, $form, FormStateInterface $form_state) } // Cache form state. - if (\Drupal::config('system.performance')->get('cache.page.use_internal') && drupal_page_is_cacheable()) { + if ($this->configFactory->get('system.performance')->get('cache.page.use_internal') && drupal_page_is_cacheable()) { $form_state->addBuildInfo('immutable', TRUE); } // Store the known list of safe strings for form re-use. diff --git a/core/tests/Drupal/Tests/Core/Form/FormTestBase.php b/core/tests/Drupal/Tests/Core/Form/FormTestBase.php index 08eaa31..6a78dcc 100644 --- a/core/tests/Drupal/Tests/Core/Form/FormTestBase.php +++ b/core/tests/Drupal/Tests/Core/Form/FormTestBase.php @@ -134,6 +134,21 @@ */ protected $httpKernel; + /** + * @var \Drupal\Core\Logger\LoggerChannelFactory; + */ + protected $logger; + + /** + * @var \Drupal\Core\Config\Config + */ + protected $config; + + /** + * @var \Drupal\Core\Config\ConfigFactoryInterface + */ + protected $configFactory; + public function setUp() { $this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface'); @@ -170,8 +185,23 @@ public function setUp() { ->setConstructorArgs(array($this->requestStack, $this->urlGenerator)) ->setMethods(array('batchGet', 'drupalInstallationAttempted')) ->getMock(); + $this->logger = $this->getMock('\Drupal\Core\Logger\LoggerChannelFactory'); - $this->formBuilder = new TestFormBuilder($this->formValidator, $this->formSubmitter, $this->moduleHandler, $this->keyValueExpirableFactory, $this->eventDispatcher, $this->requestStack, $this->classResolver, $this->csrfToken, $this->httpKernel); + $this->config = $this->getMockBuilder('\Drupal\Core\Config\Config')->disableOriginalConstructor()->getMock(); + $this->config->expects($this->any()) + ->method('get') + ->will($this->returnValueMap(array( + array('cache.page.use_internal', 1), + ))); + + $this->configFactory = $this->getMock('\Drupal\Core\Config\ConfigFactoryInterface'); + $this->configFactory->expects($this->any()) + ->method('get') + ->will($this->returnValueMap(array( + array('system.performance', $this->config), + ))); + + $this->formBuilder = new TestFormBuilder($this->formValidator, $this->formSubmitter, $this->moduleHandler, $this->keyValueExpirableFactory, $this->eventDispatcher, $this->requestStack, $this->classResolver, $this->logger, $this->configFactory, $this->csrfToken, $this->httpKernel); $this->formBuilder->setCurrentUser($this->account); } @@ -368,4 +398,9 @@ function test_form_id() { return $form; } + if (!function_exists('drupal_page_is_cacheable')) { + function drupal_page_is_cacheable($allow_caching = NULL) { + return true; + } + } }