From b0c49c821fff95ceae651f7fc3acc88de2cd6f9c Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Sat, 8 Nov 2014 16:11:40 -0800 Subject: [PATCH] Issue #2368975 by damiankloip: Fixed ElementInfoManager::buildInfo() processes info data on every request. --- core/lib/Drupal/Core/Form/FormBuilder.php | 14 +++++++++++++ core/lib/Drupal/Core/Render/Element/Form.php | 1 - .../Drupal/Core/Render/ElementInfoManager.php | 21 +++++++++++++++++++ .../Drupal/Tests/Core/Form/FormTestBase.php | 7 +++++++ .../Core/Render/ElementInfoManagerTest.php | 15 +++++++++++++ 5 files changed, 57 insertions(+), 1 deletion(-) diff --git a/core/lib/Drupal/Core/Form/FormBuilder.php b/core/lib/Drupal/Core/Form/FormBuilder.php index d3eabe2db8a2..d2040505722e 100644 --- a/core/lib/Drupal/Core/Form/FormBuilder.php +++ b/core/lib/Drupal/Core/Form/FormBuilder.php @@ -533,6 +533,11 @@ class FormBuilder implements FormBuilderInterface, FormValidatorInterface, FormS $form['#type'] = 'form'; + // Only update the action if it is not already set. + if (!isset($form['#action'])) { + $form['#action'] = $this->requestUri(); + } + // Fix the form method, if it is 'get' in $form_state, but not in $form. if ($form_state->isMethodType('get') && !isset($form['#method'])) { $form['#method'] = 'get'; @@ -1096,4 +1101,13 @@ class FormBuilder implements FormBuilderInterface, FormValidatorInterface, FormS return $this->currentUser; } + /** + * Gets the current request URI. + * + * @return string + */ + protected function requestUri() { + return request_uri(); + } + } diff --git a/core/lib/Drupal/Core/Render/Element/Form.php b/core/lib/Drupal/Core/Render/Element/Form.php index 2dbd1c77c67c..3b3864607d9a 100644 --- a/core/lib/Drupal/Core/Render/Element/Form.php +++ b/core/lib/Drupal/Core/Render/Element/Form.php @@ -22,7 +22,6 @@ class Form extends RenderElement { public function getInfo() { return array( '#method' => 'post', - '#action' => request_uri(), '#theme_wrappers' => array('form'), ); } diff --git a/core/lib/Drupal/Core/Render/ElementInfoManager.php b/core/lib/Drupal/Core/Render/ElementInfoManager.php index 784fdef1473e..1054b85285d3 100644 --- a/core/lib/Drupal/Core/Render/ElementInfoManager.php +++ b/core/lib/Drupal/Core/Render/ElementInfoManager.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Render; +use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Plugin\DefaultPluginManager; @@ -65,6 +66,12 @@ class ElementInfoManager extends DefaultPluginManager implements ElementInfoMana * Builds up all element information. */ protected function buildInfo() { + // Get cached definitions. + if ($cache = $this->cacheBackend->get('element_info_build')) { + return $cache->data; + } + + // Otherwise, rebuild and cache. // @todo Remove this hook once all elements are converted to plugins in // https://www.drupal.org/node/2311393. $info = $this->moduleHandler->invokeAll('element_info'); @@ -81,12 +88,15 @@ class ElementInfoManager extends DefaultPluginManager implements ElementInfoMana } $info[$element_type] = $element_info; } + foreach ($info as $element_type => $element) { $info[$element_type]['#type'] = $element_type; } // Allow modules to alter the element type defaults. $this->moduleHandler->alter('element_info', $info); + $this->cacheBackend->set('element_info_build', $info, Cache::PERMANENT); + return $info; } @@ -99,4 +109,15 @@ class ElementInfoManager extends DefaultPluginManager implements ElementInfoMana return parent::createInstance($plugin_id, $configuration); } + /** + * {@inheritdoc} + */ + public function clearCachedDefinitions() { + $this->elementInfo = NULL; + $this->cacheBackend->delete('element_info_build'); + + parent::clearCachedDefinitions(); + } + + } diff --git a/core/tests/Drupal/Tests/Core/Form/FormTestBase.php b/core/tests/Drupal/Tests/Core/Form/FormTestBase.php index 61c4f95bbef5..13eae9ab82aa 100644 --- a/core/tests/Drupal/Tests/Core/Form/FormTestBase.php +++ b/core/tests/Drupal/Tests/Core/Form/FormTestBase.php @@ -327,6 +327,13 @@ class TestFormBuilder extends FormBuilder { static::$seenIds = array(); } + /** + * {@inheritdoc} + */ + protected function requestUri() { + return ''; + } + } } diff --git a/core/tests/Drupal/Tests/Core/Render/ElementInfoManagerTest.php b/core/tests/Drupal/Tests/Core/Render/ElementInfoManagerTest.php index 42c9f3babbbe..ef9a9569a587 100644 --- a/core/tests/Drupal/Tests/Core/Render/ElementInfoManagerTest.php +++ b/core/tests/Drupal/Tests/Core/Render/ElementInfoManagerTest.php @@ -69,6 +69,21 @@ class ElementInfoManagerTest extends UnitTestCase { return $info; })); + $this->cache->expects($this->at(0)) + ->method('get') + ->with('element_info_build') + ->will($this->returnValue(FALSE)); + $this->cache->expects($this->at(1)) + ->method('get') + ->with('element_info') + ->will($this->returnValue(FALSE)); + $this->cache->expects($this->at(2)) + ->method('set') + ->with('element_info'); + $this->cache->expects($this->at(3)) + ->method('set') + ->with('element_info_build'); + $this->assertEquals($expected_info, $this->elementInfo->getInfo($type)); }