Skip to content

Commit 9f381c8

Browse files
authored
Action Responses (statamic#797)
1 parent 0c88527 commit 9f381c8

File tree

1 file changed

+99
-1
lines changed

1 file changed

+99
-1
lines changed

content/collections/extending-docs/actions.md

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Delete extends Action
3636

3737
The `run` method is for executing the task. You will be provided with a collection of `$items`, and any submitted `$values` (more about those later).
3838

39-
If you return a string, it will be shown in the toast notification. Otherwise, you'll get a generic success message.
39+
You may customize the outcome of the action by providing a [response](#responses).
4040

4141
### Redirects
4242

@@ -149,3 +149,101 @@ protected function fieldItems()
149149
```
150150

151151
The values entered into these fields when a user runs the action will be passed into the `run` method.
152+
153+
154+
## Responses
155+
156+
Within an action's `run` method, you may return different things.
157+
158+
### Void
159+
Returning nothing will result in a generic success toast notification saying "Action Completed".
160+
161+
```php
162+
public function run($values)
163+
{
164+
// do something, but don't return anything
165+
}
166+
```
167+
168+
### String
169+
Returning a string will customize the toast notification text.
170+
171+
```php
172+
public function run($values)
173+
{
174+
// do something
175+
176+
return __('The thing was done.');
177+
}
178+
```
179+
180+
### Array
181+
You may return an array with a `message` key in it. The message will be shown in the toast notification. Any additional keys will be passed into the event handler, useful if you are implementing your own listing component.
182+
183+
```php
184+
public function run($values)
185+
{
186+
// do something
187+
188+
return [
189+
'message' => 'This will be in the toast.',
190+
'foo' => 'bar',
191+
];
192+
}
193+
```
194+
```html
195+
<data-list-bulk-actions ... @completed="completed" />
196+
```
197+
```js
198+
completed(success, response) {
199+
this.$toast.success(response.message);
200+
this.doSomethingWithFoo(response.foo);
201+
}
202+
```
203+
204+
### Custom JavaScript Callback
205+
206+
You may return an array with a `callback` key in it. This should be an array with the name of the callback, and any arguments it should receive.
207+
208+
```php
209+
public function run($values)
210+
{
211+
// do something
212+
213+
return [
214+
'callback' => ['myCallback', 'arg1', 'arg2'],
215+
];
216+
}
217+
```
218+
219+
You can provide the callback from your JavaScript.
220+
221+
```js
222+
Statamic.$callbacks.add('myCallback', function (foo, bar) {
223+
console.log(foo, bar); // "arg1", "arg2"
224+
});
225+
```
226+
227+
:::tip
228+
A common reason for wanting to use JavaScript here is to copy a value to the user's clipboard. There's a native callback you can use so you don't need to write the JavaScript yourself:
229+
230+
```php
231+
return [
232+
'callback' => ['copyToClipboard', 'text to copy']
233+
];
234+
```
235+
:::
236+
237+
### Disabling the toast
238+
You may wish to disable the toast notification, perhaps if you are planning to trigger your own notification as part of your JavaScript callback. You can disable it by passing a value of `false`.
239+
240+
```php
241+
public function run($values)
242+
{
243+
// do something
244+
245+
return [
246+
'message' => false,
247+
];
248+
}
249+
```

0 commit comments

Comments
 (0)