Commit 37e61b15 authored by alexpott's avatar alexpott

Issue #2132477 by tkuldeep17, tim.plunkett | shameemkm: Convert batch_test forms to controllers.

parent 1b55c4f2
......@@ -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'] . '<br/>',
);
$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);
}
/**
......
......@@ -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'
......
......@@ -87,7 +87,7 @@ function testProgrammatic($value = 1) {
$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.',
......
<?php
/**
* @file
* Contains \Drupal\batch_test\Form\BatchTestChainedForm.
*/
namespace Drupal\batch_test\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Url;
/**
* Generate form of id batch_test_chained_form.
*/
class BatchTestChainedForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'batch_test_chained_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state) {
// 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(
'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');
}
}
<?php
/**
* @file
* Contains \Drupal\batch_test\Form\BatchTestForm.
*/
namespace Drupal\batch_test\Form;
/**
* Temporary form controller for batch_test module.
*/
class BatchTestForm {
/**
* @todo Remove batch_test_simple_form().
*/
public function testForm() {
return \Drupal::formBuilder()->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');
}
}
<?php
/**
* @file
* Contains \Drupal\batch_test\Form\BatchTestMockForm.
*/
namespace Drupal\batch_test\Form;
use Drupal\Core\Form\FormBase;
/**
* Generate form of id batch_test_mock_form.
*/
class BatchTestMockForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'batch_test_mock_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state) {
$form['test_value'] = array(
'#title' => 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']);
}
}
<?php
/**
* @file
* Contains \Drupal\batch_test\Form\BatchTestMultiStepForm.
*/
namespace Drupal\batch_test\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Url;
/**
* Generate form of id batch_test_multistep_form.
*/
class BatchTestMultiStepForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'batch_test_multistep_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state) {
if (empty($form_state['storage']['step'])) {
$form_state['storage']['step'] = 1;
}
$form['step_display'] = array(
'#markup' => 'step ' . $form_state['storage']['step'] . '<br/>',
);
$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');
}
}
<?php
/**
* @file
* Contains \Drupal\batch_test\Form\BatchTestSimpleForm.
*/
namespace Drupal\batch_test\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Url;
/**
* Generate form of id batch_test_simple_form.
*/
class BatchTestSimpleForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'batch_test_simple_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state) {
$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;
}
/**
* {@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');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment