diff --git a/core/lib/Drupal/Core/Form/FormBuilder.php b/core/lib/Drupal/Core/Form/FormBuilder.php index 6a81eb0..8e59e0f 100644 --- a/core/lib/Drupal/Core/Form/FormBuilder.php +++ b/core/lib/Drupal/Core/Form/FormBuilder.php @@ -190,9 +190,12 @@ public function getForm($form_arg) { /** * {@inheritdoc} */ - public function buildForm($form_id, &$form_state) { + public function buildForm($form_id, array &$form_state) { // Ensure some defaults; if already set they will not be overridden. - $form_state += $this->getFormStateDefaults(); + $form_state = NestedArray::mergeDeep($form_state, $this->getFormStateDefaults()); + + // Ensure the form ID is prepared. + $form_id = $this->getFormId($form_id, $form_state); if (!isset($form_state['input'])) { $form_state['input'] = $form_state['method'] == 'get' ? $_GET : $_POST; diff --git a/core/lib/Drupal/Core/Form/FormBuilderInterface.php b/core/lib/Drupal/Core/Form/FormBuilderInterface.php index bbe355a..b655523 100644 --- a/core/lib/Drupal/Core/Form/FormBuilderInterface.php +++ b/core/lib/Drupal/Core/Form/FormBuilderInterface.php @@ -73,7 +73,7 @@ public function getForm($form_arg); * can implement hook_forms(), which maps different $form_id values to the * proper form constructor function. Examples may be found in node_forms(), * and search_forms(). - * @param $form_state + * @param array $form_state * An array which stores information about the form. This is passed as a * reference so that the caller can use it to examine what in the form * changed when the form submission process is complete. Furthermore, it may @@ -229,7 +229,7 @@ public function getForm($form_arg); * * @see self::redirectForm() */ - public function buildForm($form_id, &$form_state); + public function buildForm($form_id, array &$form_state); /** * Retrieves default values for the $form_state array. diff --git a/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php b/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php index 2374046..9943f67 100644 --- a/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php +++ b/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php @@ -25,7 +25,7 @@ class FormBuilderTest extends UnitTestCase { /** * The form builder being tested. * - * @var \Drupal\Core\Form\FormBuilderInterface + * @var \Drupal\Core\Form\FormBuilder */ protected $formBuilder; @@ -354,6 +354,48 @@ public function testGetFormWithObject() { } /** + * Tests the getForm() method with a class name based form ID. + */ + public function testgetFormWithClassString() { + $form_id = '\Drupal\Tests\Core\Form\TestForm'; + $object = new TestForm(); + $form = array(); + $form_state = array(); + $expected_form = $object->buildForm($form, $form_state); + + $form = $this->formBuilder->getForm($form_id); + $this->assertFormElement($expected_form, $form, 'test'); + $this->assertSame('test_form', $form['#id']); + } + + /** + * Tests the buildForm() method with a string based form ID. + */ + public function testBuildFormWithString() { + $form_id = 'test_form_id'; + $expected_form = $form_id(); + + $form = $this->formBuilder->getForm($form_id); + $this->assertFormElement($expected_form, $form, 'test'); + $this->assertSame($form_id, $form['#id']); + } + + /** + * Tests the buildForm() method with a class name based form ID. + */ + public function testBuildFormWithClassString() { + $form_id = '\Drupal\Tests\Core\Form\TestForm'; + $object = new TestForm(); + $form = array(); + $form_state = array(); + $expected_form = $object->buildForm($form, $form_state); + + $form = $this->formBuilder->buildForm($form_id, $form_state); + $this->assertFormElement($expected_form, $form, 'test'); + $this->assertSame('test_form', $form['#id']); + } + + /** * Tests the buildForm() method with a form object. */ public function testBuildFormWithObject() { @@ -363,7 +405,6 @@ public function testBuildFormWithObject() { $form_arg = $this->getMockForm(NULL, $expected_form); $form_state['build_info']['callback_object'] = $form_arg; - $form_state['build_info']['args'] = array(); $form = $this->formBuilder->buildForm($form_id, $form_state); $this->assertFormElement($expected_form, $form, 'test'); @@ -388,8 +429,7 @@ public function testBuildFormWithHookForms() { ), ))); - $form_state['build_info']['args'] = array(); - + $form_state = array(); $form = $this->formBuilder->buildForm($form_id, $form_state); $this->assertFormElement($expected_form, $form, 'test'); $this->assertSame($form_id, $form_state['build_info']['form_id']); @@ -410,7 +450,6 @@ public function testRebuildForm() { // Do an initial build of the form and track the build ID. $form_state = array(); $form_state['build_info']['callback_object'] = $form_arg; - $form_state['build_info']['args'] = array(); $form = $this->formBuilder->buildForm($form_id, $form_state); $original_build_id = $form['#build_id']; @@ -682,7 +721,6 @@ public function testSendResponse() { // Do an initial build of the form and track the build ID. $form_state = array(); $form_state['build_info']['callback_object'] = $form_arg; - $form_state['build_info']['args'] = array(); $this->formBuilder->buildForm($form_id, $form_state); } @@ -855,7 +893,9 @@ public function getFormId() { return 'test_form'; } - public function buildForm(array $form, array &$form_state) { } + public function buildForm(array $form, array &$form_state) { + return test_form_id(); + } public function validateForm(array &$form, array &$form_state) { } public function submitForm(array &$form, array &$form_state) { } }