9
9
use SilverStripe \Forms \GridField \GridField_ActionProvider ;
10
10
use SilverStripe \Forms \GridField \GridField_HTMLProvider ;
11
11
use SilverStripe \Forms \GridField \GridField_URLHandler ;
12
+ use SilverStripe \Core \Injector \Injectable ;
12
13
13
14
/**
14
15
* Provide a simple way to declare buttons that affects a whole GridField
18
19
abstract class GridFieldTableButton implements GridField_HTMLProvider, GridField_ActionProvider, GridField_URLHandler
19
20
{
20
21
use ProgressiveAction;
22
+ use Injectable;
21
23
22
24
/**
23
25
* Fragment to write the button to
@@ -70,6 +72,8 @@ abstract class GridFieldTableButton implements GridField_HTMLProvider, GridField
70
72
*/
71
73
protected $ attributes = [];
72
74
75
+ public bool $ submitData = false ;
76
+
73
77
/**
74
78
* @param string $targetFragment The HTML fragment to write the button into
75
79
* @param string $buttonLabel
@@ -89,7 +93,7 @@ public function getActionName()
89
93
{
90
94
$ class = (new ReflectionClass (get_called_class ()))->getShortName ();
91
95
92
- // ! without lowercase, in does not work
96
+ // ! without lowercase, it does not work
93
97
return strtolower (str_replace ('Button ' , '' , $ class ));
94
98
}
95
99
@@ -116,6 +120,9 @@ public function getHTMLFragments($gridField)
116
120
$ action ,
117
121
[]
118
122
);
123
+ if ($ this ->submitData ) {
124
+ $ button ->submitData = true ;
125
+ }
119
126
$ button ->addExtraClass ('btn btn-secondary action_ ' . $ action );
120
127
if ($ this ->noAjax ) {
121
128
$ button ->addExtraClass ('no-ajax ' );
@@ -167,11 +174,12 @@ public function getAttribute($name)
167
174
}
168
175
169
176
/**
170
- * @param $gridField
177
+ * @param GridField $gridField
171
178
* @return array<string>
172
179
*/
173
180
public function getActions ($ gridField )
174
181
{
182
+ // $gridField is not used but required by parent class
175
183
return [$ this ->getActionName ()];
176
184
}
177
185
@@ -194,7 +202,8 @@ public function handleAction(GridField $gridField, $actionName, $arguments, $dat
194
202
}
195
203
}
196
204
197
- $ result = $ this ->handle ($ gridField , $ controller );
205
+ // Data should contain $_POST vars
206
+ $ result = $ this ->handle ($ gridField , $ controller , $ arguments , $ data );
198
207
if ((!$ result || is_string ($ result )) && $ this ->progressive ) {
199
208
// simply increment counter and let's hope last action will return something
200
209
$ step = (int )$ controller ->getRequest ()->postVar ("progress_step " );
@@ -232,9 +241,11 @@ public function handleAction(GridField $gridField, $actionName, $arguments, $dat
232
241
} else {
233
242
$ response = $ controller ->getResponse ();
234
243
$ response ->setBody ($ gridField ->forTemplate ());
235
- $ response
236
- ->addHeader ('X-Status ' , 'Action completed ' );
237
244
245
+ // Add default message if none set
246
+ if (!$ response ->getHeader ('X-Status ' )) {
247
+ $ response ->addHeader ('X-Status ' , 'Action completed ' );
248
+ }
238
249
return $ response ;
239
250
}
240
251
}
@@ -250,8 +261,11 @@ public function getURLHandlers($gridField)
250
261
}
251
262
252
263
/**
264
+ * TODO: update the actual method with the new arguments
253
265
* @param GridField $gridField
254
266
* @param Controller $controller
267
+ * @param array<mixed> $arguments
268
+ * @param array<mixed> $data
255
269
* @return mixed
256
270
*/
257
271
abstract public function handle (GridField $ gridField , Controller $ controller );
0 commit comments