From 37e61b1580c96eba07cfcc5d89e08b92bc635ef8 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Tue, 15 Jul 2014 12:02:33 +0100 Subject: [PATCH] Issue #2132477 by tkuldeep17, tim.plunkett | shameemkm: Convert batch_test forms to controllers. --- .../modules/batch_test/batch_test.module | 217 +----------------- .../modules/batch_test/batch_test.routing.yml | 6 +- .../src/Controller/BatchTestController.php | 2 +- .../src/Form/BatchTestChainedForm.php | 108 +++++++++ .../batch_test/src/Form/BatchTestForm.php | 36 --- .../batch_test/src/Form/BatchTestMockForm.php | 47 ++++ .../src/Form/BatchTestMultiStepForm.php | 67 ++++++ .../src/Form/BatchTestSimpleForm.php | 60 +++++ 8 files changed, 287 insertions(+), 256 deletions(-) create mode 100644 core/modules/system/tests/modules/batch_test/src/Form/BatchTestChainedForm.php delete mode 100644 core/modules/system/tests/modules/batch_test/src/Form/BatchTestForm.php create mode 100644 core/modules/system/tests/modules/batch_test/src/Form/BatchTestMockForm.php create mode 100644 core/modules/system/tests/modules/batch_test/src/Form/BatchTestMultiStepForm.php create mode 100644 core/modules/system/tests/modules/batch_test/src/Form/BatchTestSimpleForm.php diff --git a/core/modules/system/tests/modules/batch_test/batch_test.module b/core/modules/system/tests/modules/batch_test/batch_test.module index b97f96655daa..5438acfc54c6 100644 --- a/core/modules/system/tests/modules/batch_test/batch_test.module +++ b/core/modules/system/tests/modules/batch_test/batch_test.module @@ -5,227 +5,12 @@ * Helper module for the Batch API tests. */ -/** - * Form constructor for a batch selection form. - * - * @see batch_test_simple_form_submit() - * - * @deprecated Use \Drupal\batch_test\Form\BatchTestForm::testForm() - */ -function batch_test_simple_form() { - $form['batch'] = array( - '#type' => 'select', - '#title' => 'Choose batch', - '#options' => array( - 'batch_0' => 'batch 0', - 'batch_1' => 'batch 1', - 'batch_2' => 'batch 2', - 'batch_3' => 'batch 3', - 'batch_4' => 'batch 4', - ), - ); - $form['submit'] = array( - '#type' => 'submit', - '#value' => 'Submit', - ); - - return $form; -} - -/** - * Form submission handler for batch_test_simple_form(). - * - * @see batch_test_simple_form() - */ -function batch_test_simple_form_submit($form, &$form_state) { - batch_test_stack(NULL, TRUE); - - $function = '_batch_test_' . $form_state['values']['batch']; - batch_set($function()); - - $form_state['redirect_route']['route_name'] = 'batch_test.redirect'; -} - - -/** - * Form constructor for a multistep form. - * - * @see batch_test_multistep_form_submit() - * - * @deprecated Use \Drupal\batch_test\Form\BatchTestForm::testMultistepForm() - */ -function batch_test_multistep_form($form, &$form_state) { - if (empty($form_state['storage']['step'])) { - $form_state['storage']['step'] = 1; - } - - $form['step_display'] = array( - '#markup' => 'step ' . $form_state['storage']['step'] . '
', - ); - $form['submit'] = array( - '#type' => 'submit', - '#value' => 'Submit', - ); - - return $form; -} - -/** - * Form submission handler for batch_test_multistep_form(). - * - * @see batch_test_multistep_form() - */ -function batch_test_multistep_form_submit($form, &$form_state) { - batch_test_stack(NULL, TRUE); - - switch ($form_state['storage']['step']) { - case 1: - batch_set(_batch_test_batch_1()); - break; - case 2: - batch_set(_batch_test_batch_2()); - break; - } - - if ($form_state['storage']['step'] < 2) { - $form_state['storage']['step']++; - $form_state['rebuild'] = TRUE; - } - - // This will only be effective on the last step. - $form_state['redirect_route']['route_name'] = 'batch_test.redirect'; -} - -/** - * Form constructor for a form with chained submit callbacks. - * - * @see batch_test_chained_form_submit_1() - * @see batch_test_chained_form_submit_3() - * @see batch_test_chained_form_submit_3() - * @see batch_test_chained_form_submit_4() - * - * @deprecated Use \Drupal\batch_test\Form\BatchTestForm::testChainedForm() - */ -function batch_test_chained_form() { - // This value is used to test that $form_state persists through batched - // submit handlers. - $form['value'] = array( - '#type' => 'textfield', - '#title' => 'Value', - '#default_value' => 1, - ); - $form['submit'] = array( - '#type' => 'submit', - '#value' => 'Submit', - ); - $form['#submit'] = array( - 'batch_test_chained_form_submit_1', - 'batch_test_chained_form_submit_2', - 'batch_test_chained_form_submit_3', - 'batch_test_chained_form_submit_4', - ); - - return $form; -} - -/** - * Form submission handler #1 for batch_test_chained_form(). - * - * @see batch_test_chained_form() - */ -function batch_test_chained_form_submit_1($form, &$form_state) { - batch_test_stack(NULL, TRUE); - - batch_test_stack('submit handler 1'); - batch_test_stack('value = ' . $form_state['values']['value']); - - $form_state['values']['value']++; - batch_set(_batch_test_batch_1()); - - // This redirect should not be taken into account. - $form_state['redirect_route']['route_name'] = 'batch_test.redirect'; -} - -/** - * Form submission handler #2 for batch_test_chained_form(). - * - * @see batch_test_chained_form() - */ -function batch_test_chained_form_submit_2($form, &$form_state) { - batch_test_stack('submit handler 2'); - batch_test_stack('value = ' . $form_state['values']['value']); - - $form_state['values']['value']++; - batch_set(_batch_test_batch_2()); - - // This redirect should not be taken into account. - $form_state['redirect_route']['route_name'] = 'batch_test.redirect'; -} - -/** - * Form submission handler #3 for batch_test_chained_form(). - * - * @see batch_test_chained_form() - */ -function batch_test_chained_form_submit_3($form, &$form_state) { - batch_test_stack('submit handler 3'); - batch_test_stack('value = ' . $form_state['values']['value']); - - $form_state['values']['value']++; - - // This redirect should not be taken into account. - $form_state['redirect_route']['route_name'] = 'batch_test.redirect'; -} - -/** - * Form submission handler #4 for batch_test_chained_form(). - * - * @see batch_test_chained_form() - */ -function batch_test_chained_form_submit_4($form, &$form_state) { - batch_test_stack('submit handler 4'); - batch_test_stack('value = ' . $form_state['values']['value']); - - $form_state['values']['value']++; - batch_set(_batch_test_batch_3()); - - // This is the redirect that should prevail. - $form_state['redirect_route']['route_name'] = 'batch_test.redirect'; -} - /** * Batch operation: Submits form_test_mock_form() using drupal_form_submit(). */ function _batch_test_nested_drupal_form_submit_callback($value) { $state['values']['test_value'] = $value; - drupal_form_submit('batch_test_mock_form', $state); -} - -/** - * Form constructor for a simple form with a textfield and submit button. - * - * @see batch_test_mock_form_submit() - */ -function batch_test_mock_form($form, $form_state) { - $form['test_value'] = array( - '#title' => t('Test value'), - '#type' => 'textfield', - ); - $form['submit'] = array( - '#type' => 'submit', - '#value' => t('Submit'), - ); - - return $form; -} - -/** - * Form submission handler for batch_test_mock_form(). - * - * @see batch_test_mock_form() - */ -function batch_test_mock_form_submit($form, &$form_state) { - batch_test_stack('mock form submitted with value = ' . $form_state['values']['test_value']); + \Drupal::formBuilder()->submitForm('Drupal\batch_test\Form\BatchTestMockForm', $state); } /** diff --git a/core/modules/system/tests/modules/batch_test/batch_test.routing.yml b/core/modules/system/tests/modules/batch_test/batch_test.routing.yml index afdba91820e7..03ae5dfd4c72 100644 --- a/core/modules/system/tests/modules/batch_test/batch_test.routing.yml +++ b/core/modules/system/tests/modules/batch_test/batch_test.routing.yml @@ -34,7 +34,7 @@ batch_test.no_form: batch_test.test_form: path: '/batch-test' defaults: - _content: '\Drupal\batch_test\Form\BatchTestForm::testForm' + _form: '\Drupal\batch_test\Form\BatchTestSimpleForm' _title: 'Batch test' requirements: _access: 'TRUE' @@ -42,7 +42,7 @@ batch_test.test_form: batch_test.multistep: path: '/batch-test/multistep' defaults: - _content: '\Drupal\batch_test\Form\BatchTestForm::testMultistepForm' + _form: '\Drupal\batch_test\Form\BatchTestMultiStepForm' _title: 'Multistep' requirements: _access: 'TRUE' @@ -50,7 +50,7 @@ batch_test.multistep: batch_test.chained: path: '/batch-test/chained' defaults: - _content: '\Drupal\batch_test\Form\BatchTestForm::testChainedForm' + _form: '\Drupal\batch_test\Form\BatchTestChainedForm' _title: 'Chained' requirements: _access: 'TRUE' diff --git a/core/modules/system/tests/modules/batch_test/src/Controller/BatchTestController.php b/core/modules/system/tests/modules/batch_test/src/Controller/BatchTestController.php index 266353113c54..9afb0e90b1b2 100644 --- a/core/modules/system/tests/modules/batch_test/src/Controller/BatchTestController.php +++ b/core/modules/system/tests/modules/batch_test/src/Controller/BatchTestController.php @@ -87,7 +87,7 @@ class BatchTestController { $form_state = array( 'values' => array('value' => $value) ); - drupal_form_submit('batch_test_chained_form', $form_state); + \Drupal::formBuilder()->submitForm('Drupal\batch_test\Form\BatchTestChainedForm', $form_state); return array( 'success' => array( '#markup' => 'Got out of a programmatic batched form.', diff --git a/core/modules/system/tests/modules/batch_test/src/Form/BatchTestChainedForm.php b/core/modules/system/tests/modules/batch_test/src/Form/BatchTestChainedForm.php new file mode 100644 index 000000000000..c6603deee9ee --- /dev/null +++ b/core/modules/system/tests/modules/batch_test/src/Form/BatchTestChainedForm.php @@ -0,0 +1,108 @@ + 'textfield', + '#title' => 'Value', + '#default_value' => 1, + ); + $form['submit'] = array( + '#type' => 'submit', + '#value' => 'Submit', + ); + $form['#submit'] = array( + 'Drupal\batch_test\Form\BatchTestChainedForm::batchTestChainedFormSubmit1', + 'Drupal\batch_test\Form\BatchTestChainedForm::batchTestChainedFormSubmit2', + 'Drupal\batch_test\Form\BatchTestChainedForm::batchTestChainedFormSubmit3', + 'Drupal\batch_test\Form\BatchTestChainedForm::batchTestChainedFormSubmit4', + ); + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + } + + /** + * Form submission handler #1 for batch_test_chained_form + */ + public static function batchTestChainedFormSubmit1($form, &$form_state) { + batch_test_stack(NULL, TRUE); + + batch_test_stack('submit handler 1'); + batch_test_stack('value = ' . $form_state['values']['value']); + + $form_state['values']['value']++; + batch_set(_batch_test_batch_1()); + + $form_state['redirect_route'] = new Url('batch_test.redirect'); + } + + /** + * Form submission handler #2 for batch_test_chained_form + */ + public static function batchTestChainedFormSubmit2($form, &$form_state) { + batch_test_stack('submit handler 2'); + batch_test_stack('value = ' . $form_state['values']['value']); + + $form_state['values']['value']++; + batch_set(_batch_test_batch_2()); + + $form_state['redirect_route'] = new Url('batch_test.redirect'); + } + + /** + * Form submission handler #3 for batch_test_chained_form + */ + public static function batchTestChainedFormSubmit3($form, &$form_state) { + batch_test_stack('submit handler 3'); + batch_test_stack('value = ' . $form_state['values']['value']); + + $form_state['values']['value']++; + + $form_state['redirect_route'] = new Url('batch_test.redirect'); + } + + /** + * Form submission handler #4 for batch_test_chained_form + */ + public static function batchTestChainedFormSubmit4($form, &$form_state) { + batch_test_stack('submit handler 4'); + batch_test_stack('value = ' . $form_state['values']['value']); + + $form_state['values']['value']++; + batch_set(_batch_test_batch_3()); + + $form_state['redirect_route'] = new Url('batch_test.redirect'); + } + +} diff --git a/core/modules/system/tests/modules/batch_test/src/Form/BatchTestForm.php b/core/modules/system/tests/modules/batch_test/src/Form/BatchTestForm.php deleted file mode 100644 index fd015ff872d2..000000000000 --- a/core/modules/system/tests/modules/batch_test/src/Form/BatchTestForm.php +++ /dev/null @@ -1,36 +0,0 @@ -getForm('batch_test_simple_form'); - } - - /** - * @todo Remove batch_test_multistep_form(). - */ - public function testMultistepForm() { - return \Drupal::formBuilder()->getForm('batch_test_multistep_form'); - } - - /** - * @todo Remove batch_test_chained_form(). - */ - public function testChainedForm() { - return \Drupal::formBuilder()->getForm('batch_test_chained_form'); - } - -} diff --git a/core/modules/system/tests/modules/batch_test/src/Form/BatchTestMockForm.php b/core/modules/system/tests/modules/batch_test/src/Form/BatchTestMockForm.php new file mode 100644 index 000000000000..6372c208a396 --- /dev/null +++ b/core/modules/system/tests/modules/batch_test/src/Form/BatchTestMockForm.php @@ -0,0 +1,47 @@ + t('Test value'), + '#type' => 'textfield', + ); + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Submit'), + ); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + batch_test_stack('mock form submitted with value = ' . $form_state['values']['test_value']); + } + +} diff --git a/core/modules/system/tests/modules/batch_test/src/Form/BatchTestMultiStepForm.php b/core/modules/system/tests/modules/batch_test/src/Form/BatchTestMultiStepForm.php new file mode 100644 index 000000000000..4ce90a4ecc28 --- /dev/null +++ b/core/modules/system/tests/modules/batch_test/src/Form/BatchTestMultiStepForm.php @@ -0,0 +1,67 @@ + 'step ' . $form_state['storage']['step'] . '
', + ); + $form['submit'] = array( + '#type' => 'submit', + '#value' => 'Submit', + ); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + batch_test_stack(NULL, TRUE); + + switch ($form_state['storage']['step']) { + case 1: + batch_set(_batch_test_batch_1()); + break; + case 2: + batch_set(_batch_test_batch_2()); + break; + } + + if ($form_state['storage']['step'] < 2) { + $form_state['storage']['step']++; + $form_state['rebuild'] = TRUE; + } + + $form_state['redirect_route'] = new Url('batch_test.redirect'); + } + +} diff --git a/core/modules/system/tests/modules/batch_test/src/Form/BatchTestSimpleForm.php b/core/modules/system/tests/modules/batch_test/src/Form/BatchTestSimpleForm.php new file mode 100644 index 000000000000..3c3933941770 --- /dev/null +++ b/core/modules/system/tests/modules/batch_test/src/Form/BatchTestSimpleForm.php @@ -0,0 +1,60 @@ + 'select', + '#title' => 'Choose batch', + '#options' => array( + 'batch_0' => 'batch 0', + 'batch_1' => 'batch 1', + 'batch_2' => 'batch 2', + 'batch_3' => 'batch 3', + 'batch_4' => 'batch 4', + ), + ); + $form['submit'] = array( + '#type' => 'submit', + '#value' => 'Submit', + ); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + batch_test_stack(NULL, TRUE); + + $function = '_batch_test_' . $form_state['values']['batch']; + batch_set($function()); + + $form_state['redirect_route'] = new Url('batch_test.redirect'); + } + +}