diff --git a/core/lib/Drupal/Core/Form/FormBuilder.php b/core/lib/Drupal/Core/Form/FormBuilder.php index 4cd5c1c..8226c71 100644 --- a/core/lib/Drupal/Core/Form/FormBuilder.php +++ b/core/lib/Drupal/Core/Form/FormBuilder.php @@ -740,35 +740,35 @@ public function prepareForm($form_id, &$form, &$form_state) { if (!isset($form['#validate'])) { // Ensure that modules can rely on #validate being set. $form['#validate'] = array(); - if (isset($form_state['build_info']['callback_object'])) { - $form['#validate'][] = array($form_state['build_info']['callback_object'], 'validateForm'); - } - // Check for a handler specific to $form_id. - elseif (function_exists($form_id . '_validate')) { - $form['#validate'][] = $form_id . '_validate'; - } - // Otherwise check whether this is a shared form and whether there is a - // handler for the shared $form_id. - elseif (isset($form_state['build_info']['base_form_id']) && function_exists($form_state['build_info']['base_form_id'] . '_validate')) { - $form['#validate'][] = $form_state['build_info']['base_form_id'] . '_validate'; - } + } + if (isset($form_state['build_info']['callback_object'])) { + $form['#validate'][] = array($form_state['build_info']['callback_object'], 'validateForm'); + } + // Check for a handler specific to $form_id. + elseif (function_exists($form_id . '_validate')) { + $form['#validate'][] = $form_id . '_validate'; + } + // Otherwise check whether this is a shared form and whether there is a + // handler for the shared $form_id. + elseif (isset($form_state['build_info']['base_form_id']) && function_exists($form_state['build_info']['base_form_id'] . '_validate')) { + $form['#validate'][] = $form_state['build_info']['base_form_id'] . '_validate'; } if (!isset($form['#submit'])) { // Ensure that modules can rely on #submit being set. $form['#submit'] = array(); - if (isset($form_state['build_info']['callback_object'])) { - $form['#submit'][] = array($form_state['build_info']['callback_object'], 'submitForm'); - } - // Check for a handler specific to $form_id. - elseif (function_exists($form_id . '_submit')) { - $form['#submit'][] = $form_id . '_submit'; - } - // Otherwise check whether this is a shared form and whether there is a - // handler for the shared $form_id. - elseif (isset($form_state['build_info']['base_form_id']) && function_exists($form_state['build_info']['base_form_id'] . '_submit')) { - $form['#submit'][] = $form_state['build_info']['base_form_id'] . '_submit'; - } + } + if (isset($form_state['build_info']['callback_object'])) { + $form['#submit'][] = array($form_state['build_info']['callback_object'], 'submitForm'); + } + // Check for a handler specific to $form_id. + elseif (function_exists($form_id . '_submit')) { + $form['#submit'][] = $form_id . '_submit'; + } + // Otherwise check whether this is a shared form and whether there is a + // handler for the shared $form_id. + elseif (isset($form_state['build_info']['base_form_id']) && function_exists($form_state['build_info']['base_form_id'] . '_submit')) { + $form['#submit'][] = $form_state['build_info']['base_form_id'] . '_submit'; } // If no #theme has been set, automatically apply theme suggestions. diff --git a/core/modules/system/lib/Drupal/system/Tests/Form/FormDefaultHandlersTest.php b/core/modules/system/lib/Drupal/system/Tests/Form/FormDefaultHandlersTest.php new file mode 100644 index 0000000..481c193 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Tests/Form/FormDefaultHandlersTest.php @@ -0,0 +1,92 @@ + 'Form handlers', + 'description' => 'Tests automatically added form handlers.', + 'group' => 'Form API', + ); + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'test_form_handlers'; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, array &$form_state) { + $form['#validate'][] = array($this, 'customValidateForm'); + $form['#submit'][] = array($this, 'customSubmitForm'); + $form['submit'] = array('#type' => 'submit', '#value' => 'Save'); + return $form; + } + + /** + * {@inheritdoc} + */ + public function customValidateForm(array &$form, array &$form_state) { + $form_state['test_handlers']['validate'][] = __FUNCTION__; + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, array &$form_state) { + $form_state['test_handlers']['validate'][] = __FUNCTION__; + } + + /** + * {@inheritdoc} + */ + public function customSubmitForm(array &$form, array &$form_state) { + $form_state['test_handlers']['submit'][] = __FUNCTION__; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + $form_state['test_handlers']['submit'][] = __FUNCTION__; + } + + /** + * Tests that default handlers are added even if custom are specified. + */ + function testLimitValidationErrors() { + $form_state['values'] = array(); + $form_builder = $this->container->get('form_builder'); + $form_builder->submitForm($this, $form_state); + + $handlers = $form_state['test_handlers']; + + $this->assertIdentical(count($handlers['validate']), 2); + $this->assertIdentical($handlers['validate'][0], 'customValidateForm'); + $this->assertIdentical($handlers['validate'][1], 'validateForm'); + + $this->assertIdentical(count($handlers['submit']), 2); + $this->assertIdentical($handlers['submit'][0], 'customSubmitForm'); + $this->assertIdentical($handlers['submit'][1], 'submitForm'); + } + +}