Issue #2368975 by damiankloip: Fixed ElementInfoManager::buildInfo() processes info data on every request.

8.0.x
Alex Pott 2014-11-08 16:11:40 -08:00
parent ec7b05625a
commit b0c49c821f
5 changed files with 57 additions and 1 deletions

View File

@ -533,6 +533,11 @@ class FormBuilder implements FormBuilderInterface, FormValidatorInterface, FormS
$form['#type'] = 'form'; $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. // Fix the form method, if it is 'get' in $form_state, but not in $form.
if ($form_state->isMethodType('get') && !isset($form['#method'])) { if ($form_state->isMethodType('get') && !isset($form['#method'])) {
$form['#method'] = 'get'; $form['#method'] = 'get';
@ -1096,4 +1101,13 @@ class FormBuilder implements FormBuilderInterface, FormValidatorInterface, FormS
return $this->currentUser; return $this->currentUser;
} }
/**
* Gets the current request URI.
*
* @return string
*/
protected function requestUri() {
return request_uri();
}
} }

View File

@ -22,7 +22,6 @@ class Form extends RenderElement {
public function getInfo() { public function getInfo() {
return array( return array(
'#method' => 'post', '#method' => 'post',
'#action' => request_uri(),
'#theme_wrappers' => array('form'), '#theme_wrappers' => array('form'),
); );
} }

View File

@ -7,6 +7,7 @@
namespace Drupal\Core\Render; namespace Drupal\Core\Render;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager; use Drupal\Core\Plugin\DefaultPluginManager;
@ -65,6 +66,12 @@ class ElementInfoManager extends DefaultPluginManager implements ElementInfoMana
* Builds up all element information. * Builds up all element information.
*/ */
protected function buildInfo() { 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 // @todo Remove this hook once all elements are converted to plugins in
// https://www.drupal.org/node/2311393. // https://www.drupal.org/node/2311393.
$info = $this->moduleHandler->invokeAll('element_info'); $info = $this->moduleHandler->invokeAll('element_info');
@ -81,12 +88,15 @@ class ElementInfoManager extends DefaultPluginManager implements ElementInfoMana
} }
$info[$element_type] = $element_info; $info[$element_type] = $element_info;
} }
foreach ($info as $element_type => $element) { foreach ($info as $element_type => $element) {
$info[$element_type]['#type'] = $element_type; $info[$element_type]['#type'] = $element_type;
} }
// Allow modules to alter the element type defaults. // Allow modules to alter the element type defaults.
$this->moduleHandler->alter('element_info', $info); $this->moduleHandler->alter('element_info', $info);
$this->cacheBackend->set('element_info_build', $info, Cache::PERMANENT);
return $info; return $info;
} }
@ -99,4 +109,15 @@ class ElementInfoManager extends DefaultPluginManager implements ElementInfoMana
return parent::createInstance($plugin_id, $configuration); return parent::createInstance($plugin_id, $configuration);
} }
/**
* {@inheritdoc}
*/
public function clearCachedDefinitions() {
$this->elementInfo = NULL;
$this->cacheBackend->delete('element_info_build');
parent::clearCachedDefinitions();
}
} }

View File

@ -327,6 +327,13 @@ class TestFormBuilder extends FormBuilder {
static::$seenIds = array(); static::$seenIds = array();
} }
/**
* {@inheritdoc}
*/
protected function requestUri() {
return '';
}
} }
} }

View File

@ -69,6 +69,21 @@ class ElementInfoManagerTest extends UnitTestCase {
return $info; 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)); $this->assertEquals($expected_info, $this->elementInfo->getInfo($type));
} }