diff --git a/core/includes/common.inc b/core/includes/common.inc index 7ace7e1..26ed25f 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -1213,7 +1213,7 @@ function element_info($type) { * Retrieves a single property for the defined element type. * * @param $type - * An element type as defined by an element plugin. + * An element type as defined by hook_element_info() or an element plugin. * @param $property_name * The property within the element type that should be returned. * @param $default diff --git a/core/lib/Drupal/Core/Render/ElementInfoManager.php b/core/lib/Drupal/Core/Render/ElementInfoManager.php index bf9b971..a357362 100644 --- a/core/lib/Drupal/Core/Render/ElementInfoManager.php +++ b/core/lib/Drupal/Core/Render/ElementInfoManager.php @@ -110,7 +110,10 @@ protected function buildInfo($theme_name) { } // Otherwise, rebuild and cache. - $info = []; + // @todo Remove this hook once hook_element_info() is removed in + // https://www.drupal.org/node/2311393. + $info = $this->moduleHandler->invokeAll('element_info'); + foreach ($this->getDefinitions() as $element_type => $definition) { $element = $this->createInstance($element_type); $element_info = $element->getInfo(); diff --git a/core/lib/Drupal/Core/Render/ElementInfoManagerInterface.php b/core/lib/Drupal/Core/Render/ElementInfoManagerInterface.php index 0b98d82..6c114d8 100644 --- a/core/lib/Drupal/Core/Render/ElementInfoManagerInterface.php +++ b/core/lib/Drupal/Core/Render/ElementInfoManagerInterface.php @@ -44,6 +44,8 @@ * - #title_display: optional string indicating if and how #title should be * displayed (see form-element.html.twig). * + * @see hook_element_info() + * @see hook_element_info_alter() * @see \Drupal\Core\Render\Element\ElementInterface * @see \Drupal\Core\Render\Element\ElementInterface::getInfo() */ diff --git a/core/tests/Drupal/Tests/Core/Render/ElementInfoManagerTest.php b/core/tests/Drupal/Tests/Core/Render/ElementInfoManagerTest.php index e0d3717..b0ca1f7 100644 --- a/core/tests/Drupal/Tests/Core/Render/ElementInfoManagerTest.php +++ b/core/tests/Drupal/Tests/Core/Render/ElementInfoManagerTest.php @@ -67,6 +67,101 @@ protected function setUp() { } /** + * Tests the getInfo method. + * + * @covers ::getInfo + * @covers ::buildInfo + * + * @dataProvider providerTestGetInfo + */ + public function testGetInfo($type, $expected_info, $element_info, callable $alter_callback = NULL) { + $this->moduleHandler->expects($this->once()) + ->method('invokeAll') + ->with('element_info') + ->will($this->returnValue($element_info)); + $this->moduleHandler->expects($this->once()) + ->method('alter') + ->with('element_info', $this->anything()) + ->will($this->returnCallback($alter_callback ?: function($info) { + return $info; + })); + $this->themeManager->expects($this->once()) + ->method('getActiveTheme') + ->willReturn(new ActiveTheme(['name' => 'test'])); + $this->themeManager->expects($this->once()) + ->method('alter') + ->with('element_info', $this->anything()) + ->will($this->returnCallback($alter_callback ?: function($info) { + return $info; + })); + + $this->cache->expects($this->at(0)) + ->method('get') + ->with('element_info_build:test') + ->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:test'); + + $this->assertEquals($expected_info, $this->elementInfo->getInfo($type)); + } + + /** + * Provides tests data for getInfo. + * + * @return array + */ + public function providerTestGetInfo() { + $data = array(); + // Provide an element and expect it is returned. + $data[] = array( + 'page', + array( + '#type' => 'page', + '#theme' => 'page', + '#defaults_loaded' => TRUE, + ), + array('page' => array( + '#theme' => 'page', + )), + ); + // Provide an element but request an non existent one. + $data[] = array( + 'form', + array( + '#defaults_loaded' => TRUE, + ), + array('page' => array( + '#theme' => 'page', + )), + ); + // Provide an element and alter it to ensure it is altered. + $data[] = array( + 'page', + array( + '#type' => 'page', + '#theme' => 'page', + '#number' => 597219, + '#defaults_loaded' => TRUE, + ), + array('page' => array( + '#theme' => 'page', + )), + function ($alter_name, array &$info) { + $info['page']['#number'] = 597219; + } + ); + return $data; + } + + /** * Tests the getInfo() method when render element plugins are used. * * @covers ::getInfo @@ -76,6 +171,10 @@ protected function setUp() { */ public function testGetInfoElementPlugin($plugin_class, $expected_info) { $this->moduleHandler->expects($this->once()) + ->method('invokeAll') + ->with('element_info') + ->willReturn(array()); + $this->moduleHandler->expects($this->once()) ->method('alter') ->with('element_info', $this->anything()) ->will($this->returnArgument(0));