You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/collections/extending-docs/actions.md
+99-1Lines changed: 99 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -36,7 +36,7 @@ class Delete extends Action
36
36
37
37
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).
38
38
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).
40
40
41
41
### Redirects
42
42
@@ -149,3 +149,101 @@ protected function fieldItems()
149
149
```
150
150
151
151
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.
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`.
0 commit comments