diff --git a/core/lib/Drupal/Core/HtmlFormController.php b/core/lib/Drupal/Core/HtmlFormController.php index 5703c14..b641341 100644 --- a/core/lib/Drupal/Core/HtmlFormController.php +++ b/core/lib/Drupal/Core/HtmlFormController.php @@ -46,11 +46,14 @@ public function setContainer(ContainerInterface $container = NULL) { * A response object. */ public function content(Request $request, $_form) { - // @todo Add the ability for forms to have a factory method just like - // controllers. See http://drupal.org/node/1915774#comment-7140912. // If this is a class, instantiate it. if (class_exists($_form)) { - $form_arg = new $_form(); + if (in_array('Drupal\Core\ControllerInterface', class_implements($_form))) { + $form_arg = $_form::create($this->container); + } + else { + $form_arg = new $_form(); + } } // Otherwise, it is a service. else { diff --git a/core/modules/system/lib/Drupal/system/Tests/Form/FormObjectTest.php b/core/modules/system/lib/Drupal/system/Tests/Form/FormObjectTest.php index 58d2ca4..39a9b86 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Form/FormObjectTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Form/FormObjectTest.php @@ -78,6 +78,17 @@ function testObjectFormCallback() { $this->assertText('The FormTestServiceObject::submitForm() method was used for this form.'); $value = $config_factory->get('form_test.object')->get('bananas'); $this->assertIdentical('brown', $value); + + $this->drupalGet('form-test/object-controller-builder'); + $this->assertText('The FormTestControllerObject::create() method was used for this form.'); + $this->assertText('The FormTestControllerObject::buildForm() method was used for this form.'); + $elements = $this->xpath('//form[@id="form-test-form-test-controller-object"]'); + $this->assertTrue(!empty($elements), 'The correct form ID was used.'); + $this->drupalPost(NULL, array('bananas' => 'black'), t('Save')); + $this->assertText('The FormTestControllerObject::validateForm() method was used for this form.'); + $this->assertText('The FormTestControllerObject::submitForm() method was used for this form.'); + $value = $config_factory->get('form_test.object')->get('bananas'); + $this->assertIdentical('black', $value); } } diff --git a/core/modules/system/tests/modules/condition_test/condition_test.routing.yml b/core/modules/system/tests/modules/condition_test/condition_test.routing.yml index 96fbdca..d242511 100644 --- a/core/modules/system/tests/modules/condition_test/condition_test.routing.yml +++ b/core/modules/system/tests/modules/condition_test/condition_test.routing.yml @@ -1,6 +1,6 @@ condition_test_1: pattern: '/condition_test' defaults: - _controller: '\Drupal\condition_test\FormController::getForm' + _form: '\Drupal\condition_test\FormController' requirements: _access: 'TRUE' diff --git a/core/modules/system/tests/modules/condition_test/lib/Drupal/condition_test/FormController.php b/core/modules/system/tests/modules/condition_test/lib/Drupal/condition_test/FormController.php index 03b918e..93c3603 100644 --- a/core/modules/system/tests/modules/condition_test/lib/Drupal/condition_test/FormController.php +++ b/core/modules/system/tests/modules/condition_test/lib/Drupal/condition_test/FormController.php @@ -30,12 +30,11 @@ public function getFormID() { } /** - * Provides a simple method the router can fire in order to invoke this form. + * Constructs a \Drupal\condition_test\FormController object. */ - public function getForm() { + public function __construct() { $manager = new ConditionManager(drupal_container()->getParameter('container.namespaces')); $this->condition = $manager->createInstance('node_type'); - return drupal_get_form($this); } /** diff --git a/core/modules/system/tests/modules/form_test/form_test.routing.yml b/core/modules/system/tests/modules/form_test/form_test.routing.yml index 6c392b6..826e737 100644 --- a/core/modules/system/tests/modules/form_test/form_test.routing.yml +++ b/core/modules/system/tests/modules/form_test/form_test.routing.yml @@ -18,3 +18,10 @@ form_test.route3: _form: 'form_test.form.serviceForm' requirements: _access: 'TRUE' + +form_test.route4: + pattern: '/form-test/object-controller-builder' + defaults: + _form: '\Drupal\form_test\FormTestControllerObject' + requirements: + _access: 'TRUE' diff --git a/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestControllerObject.php b/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestControllerObject.php new file mode 100644 index 0000000..38a8fc7 --- /dev/null +++ b/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestControllerObject.php @@ -0,0 +1,70 @@ + 'The FormTestControllerObject::buildForm() method was used for this form.'); + + $form['bananas'] = array( + '#type' => 'textfield', + '#title' => t('Bananas'), + ); + + $form['actions']['#type'] = 'actions'; + $form['actions']['submit'] = array( + '#type' => 'submit', + '#value' => t('Save'), + ); + return $form; + } + + /** + * Implements \Drupal\Core\Form\FormInterface::validateForm(). + */ + public function validateForm(array &$form, array &$form_state) { + drupal_set_message(t('The FormTestControllerObject::validateForm() method was used for this form.')); + } + + /** + * Implements \Drupal\Core\Form\FormInterface::submitForm(). + */ + public function submitForm(array &$form, array &$form_state) { + drupal_set_message(t('The FormTestControllerObject::submitForm() method was used for this form.')); + config('form_test.object') + ->set('bananas', $form_state['values']['bananas']) + ->save(); + } + +} diff --git a/core/modules/views/views_ui/lib/Drupal/views_ui/Form/BreakLockForm.php b/core/modules/views/views_ui/lib/Drupal/views_ui/Form/BreakLockForm.php index d7f6d53..cc699b8 100644 --- a/core/modules/views/views_ui/lib/Drupal/views_ui/Form/BreakLockForm.php +++ b/core/modules/views/views_ui/lib/Drupal/views_ui/Form/BreakLockForm.php @@ -58,19 +58,6 @@ public static function create(ContainerInterface $container) { } /** - * Creates a new instance of this form. - * - * @param \Drupal\views\ViewStorageInterface $view - * The view being acted upon. - * - * @return array - * The built form array. - */ - public function getForm(ViewStorageInterface $view) { - return drupal_get_form($this, $view); - } - - /** * Implements \Drupal\Core\Form\FormInterface::getFormID(). */ public function getFormID() { diff --git a/core/modules/views/views_ui/lib/Drupal/views_ui/Form/DeleteForm.php b/core/modules/views/views_ui/lib/Drupal/views_ui/Form/DeleteForm.php index 3f38679..f9b7272 100644 --- a/core/modules/views/views_ui/lib/Drupal/views_ui/Form/DeleteForm.php +++ b/core/modules/views/views_ui/lib/Drupal/views_ui/Form/DeleteForm.php @@ -16,19 +16,6 @@ class DeleteForm implements FormInterface { /** - * Creates a new instance of this form. - * - * @param \Drupal\views\ViewStorageInterface $view - * The view being acted upon. - * - * @return array - * The built form array. - */ - public function getForm(ViewStorageInterface $view) { - return drupal_get_form($this, $view); - } - - /** * Implements \Drupal\Core\Form\FormInterface::getFormID(). */ public function getFormID() { diff --git a/core/modules/views/views_ui/lib/Drupal/views_ui/Form/SettingsFormBase.php b/core/modules/views/views_ui/lib/Drupal/views_ui/Form/SettingsFormBase.php index 82ea585..662e00d 100644 --- a/core/modules/views/views_ui/lib/Drupal/views_ui/Form/SettingsFormBase.php +++ b/core/modules/views/views_ui/lib/Drupal/views_ui/Form/SettingsFormBase.php @@ -44,14 +44,4 @@ public static function create(ContainerInterface $container) { ); } - /** - * Creates a new instance of this form. - * - * @return array - * The built form array. - */ - public function getForm() { - return drupal_get_form($this); - } - } diff --git a/core/modules/views/views_ui/views_ui.routing.yml b/core/modules/views/views_ui/views_ui.routing.yml index ed81159..9c64891 100644 --- a/core/modules/views/views_ui/views_ui.routing.yml +++ b/core/modules/views/views_ui/views_ui.routing.yml @@ -15,14 +15,14 @@ views_ui.add: views_ui.settings.basic: pattern: '/admin/structure/views/settings' defaults: - _controller: '\Drupal\views_ui\Form\BasicSettingsForm::getForm' + _form: '\Drupal\views_ui\Form\BasicSettingsForm' requirements: _permission: 'administer views' views_ui.settings.advanced: pattern: '/admin/structure/views/settings/advanced' defaults: - _controller: '\Drupal\views_ui\Form\AdvancedSettingsForm::getForm' + _form: '\Drupal\views_ui\Form\AdvancedSettingsForm' requirements: _permission: 'administer views' @@ -58,7 +58,7 @@ views_ui.clone: views_ui.delete: pattern: '/admin/structure/views/view/{view}/delete' defaults: - _controller: 'Drupal\views_ui\Form\DeleteForm::getForm' + _form: 'Drupal\views_ui\Form\DeleteForm' requirements: _permission: 'administer views' @@ -104,7 +104,7 @@ views_ui.preview: views_ui.breakLock: pattern: '/admin/structure/views/view/{view}/break-lock' defaults: - _controller: '\Drupal\views_ui\Form\BreakLockForm::getForm' + _form: '\Drupal\views_ui\Form\BreakLockForm' display_id: NULL requirements: _permission: 'administer views'