Skip to content

Commit b64d332

Browse files
committed
Issue #3495514 by nikolay shapovalov, nicxvan: Move helpers in form_test.module and delete it
1 parent 8ad8c4c commit b64d332

File tree

7 files changed

+63
-87
lines changed

7 files changed

+63
-87
lines changed

.phpstan-baseline.php

-18
Original file line numberDiff line numberDiff line change
@@ -36980,24 +36980,6 @@
3698036980
'count' => 1,
3698136981
'path' => __DIR__ . '/modules/system/tests/modules/experimental_module_test/src/Hook/ExperimentalModuleTestHooks.php',
3698236982
];
36983-
$ignoreErrors[] = [
36984-
// identifier: missingType.return
36985-
'message' => '#^Function _form_test_tableselect_get_data\\(\\) has no return type specified\\.$#',
36986-
'count' => 1,
36987-
'path' => __DIR__ . '/modules/system/tests/modules/form_test/form_test.module',
36988-
];
36989-
$ignoreErrors[] = [
36990-
// identifier: missingType.return
36991-
'message' => '#^Function form_test_tableselect_ajax_callback\\(\\) has no return type specified\\.$#',
36992-
'count' => 1,
36993-
'path' => __DIR__ . '/modules/system/tests/modules/form_test/form_test.module',
36994-
];
36995-
$ignoreErrors[] = [
36996-
// identifier: missingType.return
36997-
'message' => '#^Function form_test_user_register_form_rebuild\\(\\) has no return type specified\\.$#',
36998-
'count' => 1,
36999-
'path' => __DIR__ . '/modules/system/tests/modules/form_test/form_test.module',
37000-
];
3700136983
$ignoreErrors[] = [
3700236984
// identifier: missingType.return
3700336985
'message' => '#^Method Drupal\\\\form_test\\\\Callbacks\\:\\:validateName\\(\\) has no return type specified\\.$#',

modules/system/tests/modules/form_test/form_test.module

-63
This file was deleted.

modules/system/tests/modules/form_test/src/Callbacks.php

+46
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,50 @@ public function validateName(&$element, FormStateInterface $form_state) {
4747
}
4848
}
4949

50+
/**
51+
* Create a header and options array. Helper function for callbacks.
52+
*/
53+
public static function tableselectGetData(): array {
54+
$header = [
55+
'one' => t('One'),
56+
'two' => t('Two'),
57+
'three' => t('Three'),
58+
'four' => t('Four'),
59+
];
60+
61+
$options['row1'] = [
62+
'title' => ['data' => ['#title' => t('row1')]],
63+
'one' => 'row1col1',
64+
'two' => t('row1col2'),
65+
'three' => t('row1col3'),
66+
'four' => t('row1col4'),
67+
];
68+
69+
$options['row2'] = [
70+
'title' => ['data' => ['#title' => t('row2')]],
71+
'one' => 'row2col1',
72+
'two' => t('row2col2'),
73+
'three' => t('row2col3'),
74+
'four' => t('row2col4'),
75+
];
76+
77+
$options['row3'] = [
78+
'title' => ['data' => ['#title' => t('row3')]],
79+
'one' => 'row3col1',
80+
'two' => t('row3col2'),
81+
'three' => t('row3col3'),
82+
'four' => t('row3col4'),
83+
];
84+
85+
return [$header, $options];
86+
}
87+
88+
/**
89+
* Submit callback that just lets the form rebuild.
90+
*/
91+
public static function userRegisterFormRebuild(array $form, FormStateInterface $form_state): void {
92+
\Drupal::messenger()->addStatus('Form rebuilt.');
93+
$form_state->setRebuild();
94+
}
95+
5096
}

modules/system/tests/modules/form_test/src/Form/FormTestTableSelectColspanForm.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Drupal\form_test\Form;
66

77
use Drupal\Core\Form\FormStateInterface;
8+
use Drupal\form_test\Callbacks;
89

910
/**
1011
* Builds a form to test table selects with different column spans.
@@ -24,7 +25,7 @@ public function getFormId() {
2425
* {@inheritdoc}
2526
*/
2627
public function buildForm(array $form, FormStateInterface $form_state) {
27-
[$header, $options] = _form_test_tableselect_get_data();
28+
[$header, $options] = Callbacks::tableselectGetData();
2829

2930
// Change the data so that the third column has colspan=2.
3031
$header['three'] = ['data' => 'Three', 'colspan' => 2];

modules/system/tests/modules/form_test/src/Form/FormTestTableSelectFormBase.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Drupal\Core\Form\FormBase;
88
use Drupal\Core\Form\FormStateInterface;
9+
use Drupal\form_test\Callbacks;
910

1011
/**
1112
* Provides a base class for tableselect forms.
@@ -28,7 +29,7 @@ abstract class FormTestTableSelectFormBase extends FormBase {
2829
* A form with a tableselect element and a submit button.
2930
*/
3031
public function tableselectFormBuilder($form, FormStateInterface $form_state, $element_properties) {
31-
[$header, $options] = _form_test_tableselect_get_data();
32+
[$header, $options] = Callbacks::tableselectGetData();
3233

3334
$form['tableselect'] = $element_properties;
3435

@@ -41,7 +42,7 @@ public function tableselectFormBuilder($form, FormStateInterface $form_state, $e
4142
'#multiple' => FALSE,
4243
'#empty' => t('Empty text.'),
4344
'#ajax' => [
44-
'callback' => 'form_test_tableselect_ajax_callback',
45+
'callback' => '::tableselectAjaxCallback',
4546
'wrapper' => 'tableselect-wrapper',
4647
],
4748
];
@@ -54,4 +55,11 @@ public function tableselectFormBuilder($form, FormStateInterface $form_state, $e
5455
return $form;
5556
}
5657

58+
/**
59+
* Ajax callback that returns the form element.
60+
*/
61+
public function tableselectAjaxCallback(array $form, FormStateInterface $form_state): array {
62+
return $form['tableselect'];
63+
}
64+
5765
}

modules/system/tests/modules/form_test/src/Hook/FormTestHooks.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Drupal\Core\Form\FormStateInterface;
88
use Drupal\Core\Hook\Attribute\Hook;
9+
use Drupal\form_test\Callbacks;
910

1011
/**
1112
* Hook implementations for form_test.
@@ -55,7 +56,7 @@ public function formUserRegisterFormAlter(&$form, FormStateInterface $form_state
5556
'#type' => 'submit',
5657
'#value' => t('Rebuild'),
5758
'#submit' => [
58-
'form_test_user_register_form_rebuild',
59+
[Callbacks::class, 'userRegisterFormRebuild'],
5960
],
6061
];
6162
}

modules/system/tests/src/Functional/Form/ElementsTableSelectTest.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Drupal\Tests\system\Functional\Form;
66

77
use Drupal\Core\Form\FormState;
8+
use Drupal\form_test\Callbacks;
89
use Drupal\Tests\BrowserTestBase;
910

1011
/**
@@ -154,7 +155,7 @@ public function testAdvancedSelect(): void {
154155
*/
155156
public function testMultipleTrueOptionChecker(): void {
156157

157-
[$header, $options] = _form_test_tableselect_get_data();
158+
[$header, $options] = Callbacks::tableselectGetData();
158159

159160
$form['tableselect'] = [
160161
'#type' => 'tableselect',
@@ -177,7 +178,7 @@ public function testMultipleTrueOptionChecker(): void {
177178
*/
178179
public function testMultipleFalseOptionChecker(): void {
179180

180-
[$header, $options] = _form_test_tableselect_get_data();
181+
[$header, $options] = Callbacks::tableselectGetData();
181182

182183
$form['tableselect'] = [
183184
'#type' => 'tableselect',

0 commit comments

Comments
 (0)