2
2
3
3
namespace Drupal \commerce_payment \Element ;
4
4
5
+ use Drupal \commerce \Element \CommerceElementBase ;
5
6
use Drupal \commerce_payment \Entity \EntityWithPaymentGatewayInterface ;
6
7
use Drupal \commerce_payment \Exception \PaymentGatewayException ;
7
8
use Drupal \Core \Form \FormStateInterface ;
8
- use Drupal \Core \Render \Element ;
9
- use Drupal \Core \Render \Element \RenderElement ;
10
9
11
10
/**
12
11
* Provides a form element for embedding the payment gateway forms.
25
24
*
26
25
* @RenderElement("commerce_payment_gateway_form")
27
26
*/
28
- class PaymentGatewayForm extends RenderElement {
27
+ class PaymentGatewayForm extends CommerceElementBase {
29
28
30
29
/**
31
30
* {@inheritdoc}
@@ -37,12 +36,14 @@ public function getInfo() {
37
36
// The entity operated on. Instance of EntityWithPaymentGatewayInterface.
38
37
'#default_value ' => NULL ,
39
38
'#process ' => [
39
+ [$ class , 'attachElementSubmit ' ],
40
40
[$ class , 'processForm ' ],
41
41
],
42
42
'#element_validate ' => [
43
+ [$ class , 'validateElementSubmit ' ],
43
44
[$ class , 'validateForm ' ],
44
45
],
45
- '#element_submit ' => [
46
+ '#commerce_element_submit ' => [
46
47
[$ class , 'submitForm ' ],
47
48
],
48
49
'#theme_wrappers ' => ['container ' ],
@@ -82,10 +83,6 @@ public static function processForm(array $element, FormStateInterface $form_stat
82
83
if (isset ($ element ['#page_title ' ])) {
83
84
$ complete_form ['#title ' ] = $ element ['#page_title ' ];
84
85
}
85
- // The #validate callbacks of the complete form run last.
86
- // That allows executeElementSubmitHandlers() to be completely certain that
87
- // the form has passed validation before proceeding.
88
- $ complete_form ['#validate ' ][] = [get_class (), 'executeElementSubmitHandlers ' ];
89
86
90
87
return $ element ;
91
88
}
@@ -97,18 +94,8 @@ public static function processForm(array $element, FormStateInterface $form_stat
97
94
* The form element.
98
95
* @param \Drupal\Core\Form\FormStateInterface $form_state
99
96
* The current state of the form.
100
- *
101
- * @throws \Exception
102
- * Thrown if button-level #validate handlers are detected on the parent
103
- * form, as a protection against buggy behavior.
104
97
*/
105
98
public static function validateForm (array &$ element , FormStateInterface $ form_state ) {
106
- // Button-level #validate handlers replace the form-level ones, which means
107
- // that executeElementSubmitHandlers() won't be triggered.
108
- if ($ handlers = $ form_state ->getValidateHandlers ()) {
109
- throw new \Exception ('The commerce_payment_gateway_form element is not compatible with submit buttons that set #validate handlers ' );
110
- }
111
-
112
99
$ plugin_form = self ::createPluginForm ($ element );
113
100
$ plugin_form ->validateConfigurationForm ($ element , $ form_state );
114
101
}
@@ -144,7 +131,7 @@ public static function submitForm(array &$element, FormStateInterface $form_stat
144
131
* The plugin form.
145
132
*/
146
133
public static function createPluginForm (array $ element ) {
147
- /** @var \Drupal\commerce\PluginForm \PluginFormFactoryInterface $plugin_form_factory */
134
+ /** @var \Drupal\Core\Plugin \PluginFormFactoryInterface $plugin_form_factory */
148
135
$ plugin_form_factory = \Drupal::service ('plugin_form.factory ' );
149
136
/** @var \Drupal\commerce_payment\Entity\EntityWithPaymentGatewayInterface $entity */
150
137
$ entity = $ element ['#default_value ' ];
@@ -156,38 +143,4 @@ public static function createPluginForm(array $element) {
156
143
return $ plugin_form ;
157
144
}
158
145
159
- /**
160
- * Submits elements by calling their #element_submit callbacks.
161
- *
162
- * Form API has no #element_submit, requiring us to simulate it by running
163
- * our #element_submit handlers either in the last step of validation, or the
164
- * first step of submission. In this case it's the last step of validation,
165
- * allowing exceptions thrown by the plugin to be converted into form errors.
166
- *
167
- * @param array $element
168
- * The form element.
169
- * @param \Drupal\Core\Form\FormStateInterface $form_state
170
- * The form state.
171
- */
172
- public static function executeElementSubmitHandlers (array &$ element , FormStateInterface $ form_state ) {
173
- if (!$ form_state ->isSubmitted () || $ form_state ->hasAnyErrors ()) {
174
- // The form wasn't submitted (#ajax in progress) or failed validation.
175
- return ;
176
- }
177
-
178
- // Recurse through all children.
179
- foreach (Element::children ($ element ) as $ key ) {
180
- if (!empty ($ element [$ key ])) {
181
- static ::executeElementSubmitHandlers ($ element [$ key ], $ form_state );
182
- }
183
- }
184
-
185
- // If there are callbacks on this level, run them.
186
- if (!empty ($ element ['#element_submit ' ])) {
187
- foreach ($ element ['#element_submit ' ] as $ callback ) {
188
- call_user_func_array ($ callback , [&$ element , &$ form_state ]);
189
- }
190
- }
191
- }
192
-
193
146
}
0 commit comments