Skip to content

Commit 9e2f8a2

Browse files
authored
add save action docs (#640)
* add save action docs
1 parent 6f1f035 commit 9e2f8a2

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

7.x-dev/base-components.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ This component helps you show a form _anywhere you want_, so the admin can easil
100100
- `operation='create'` - by default, the datatable component will pick up everything that controller sets up for the Create operation; if you want to change the operation it will initialize, you can pass this parameter;
101101
- `:entry="\App\Models\Invoice::find(1)"` - if you want to use UpdateOperation or a custom form operation that needs the entry;
102102
- `:setup="function($crud, $parent) {}"` - if you want to make changes to the operation setup (eg. add/remove fields, configure functionality), you can use this parameter; the closure passed here will be run _after_ the setup of that operation had already completed;
103+
- `:save-actions="[]"` - provide an array of save action definitions or save action classes to replace the defaults (see [Custom save actions](#dataform-custom-save-actions));
104+
- `:form-inside-card="true"` - render the form inside a Backpack card wrapper so it visually matches the default create/update screens; leave it `false` to output only the raw form markup.
103105

104106
**Advanced example:**
105107

@@ -115,6 +117,33 @@ This component helps you show a form _anywhere you want_, so the admin can easil
115117
/>
116118
```
117119

120+
<a name="dataform-custom-save-actions"></a>
121+
#### Custom save actions
122+
123+
The Dataform component can swap out the default `Save and back / edit / new` buttons with your own logic. Pass an array to the `:save-actions` attribute containing save action classes (or definitions) that implement Backpack's `SaveActionInterface`:
124+
125+
```php
126+
@php
127+
use App\Backpack\Crud\SaveActions\SaveAndApprove;
128+
use Backpack\CRUD\app\Library\CrudPanel\SaveActions\SaveAndBack;
129+
@endphp
130+
131+
<x-bp-dataform
132+
controller="\App\Http\Controllers\Admin\InvoiceCrudController"
133+
:save-actions="[
134+
new SaveAndApprove,
135+
SaveAndBack::class,
136+
]"
137+
/>
138+
```
139+
140+
Each entry in the array can be:
141+
- an instance of a class that implements `SaveActionInterface` (recommended);
142+
- the fully qualified class name of a save action (the container will resolve it);
143+
- a plain array definition (see [`crud-save-actions.md`](crud-save-actions.md)).
144+
145+
Backpack will replace the default actions for that form, honour the order defined by each class, and fallback to the first action if no default applies.
146+
118147
<hr>
119148

120149
<a name="dataform-modal"></a>
@@ -140,6 +169,7 @@ use \Backpack\DataformModal\Http\Controllers\Operations\CreateInModalOperation;
140169
- `operation='createInModal'` - by default, the component will pick up everything that controller sets up for the Create operation; if you want to change the operation it will initialize, you can pass this parameter, eg: `updateInModal`
141170
- `:entry="\App\Models\Invoice::find(1)"` - if you want to use UpdateInModalOperation or a custom form operation that needs the entry;
142171
- `:setup="function($crud, $parent) {}"` - if you want to make changes to the operation setup (eg. add/remove fields, configure functionality), you can use this parameter; the closure passed here will be run _after_ the setup of that operation had already completed;
172+
- `:save-actions="[]"` - replace the default modal buttons with your own save action classes.
143173

144174
**Advanced example:**
145175

@@ -172,6 +202,7 @@ Useful if you want to show the entries in the database, for an Eloquent model. T
172202
**Configuration options:**
173203
- `name='invoices_datatable'` - by default, a name will be generated; but you can pick one you can recognize;
174204
- `operation='list'` - by default, the datatable component will pick up everything that controller sets up for the List operation; if you want to change the operation it will initialize, you can pass this parameter;
205+
- `:useFixedHeader="false"` - set this to explicitly enable or disable the sticky header; it defaults to the operation's `useFixedHeader` setting, falling back to `true`;
175206
- `:setup="function($crud, $parent) {}"` - if you want to make changes to the operation setup (eg. add/remove columns, configure functionality), you can use this parameter; the closure passed here will be run _after_ the setup of that operation had already completed;
176207

177208
**Advanced example:**

7.x-dev/crud-save-actions.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,91 @@ There are four save actions registered by Backpack by default. They are:
1616
- ```save_and_new``` (Save and go to create new entity page)
1717
- ```save_and_preview``` (Save and go to show the current entity)
1818

19+
<a name="save-action-classes"></a>
20+
## Save Action Classes
21+
22+
Save actions are now first-class citizens. Instead of maintaining large array definitions in each CrudController, you can encapsulate the behaviour inside PHP classes that implement `Backpack\CRUD\app\Library\CrudPanel\SaveActions\SaveActionInterface`. Backpack ships with `SaveAndBack`, `SaveAndEdit`, `SaveAndNew`, and `SaveAndPreview` as examples, and also provides `SaveAndList` for projects that want an explicit "Save and go to list" button.
23+
24+
### Quick start
25+
26+
1. **Create a class** inside your application (for example `app/Backpack/Crud/SaveActions/SaveAndApprove.php`).
27+
2. **Extend** `AbstractSaveAction` (recommended) or implement `SaveActionInterface` directly.
28+
3. **Override** the methods that describe your button.
29+
4. **Register** the class with `CRUD::addSaveAction()` / `CRUD::replaceSaveActions()` or pass it to Blade components like `<x-bp-dataform>`.
30+
31+
```php
32+
<?php
33+
34+
namespace App\Backpack\Crud\SaveActions;
35+
36+
use Backpack\CRUD\app\Library\CrudPanel\CrudPanel;
37+
use Backpack\CRUD\app\Library\CrudPanel\SaveActions\AbstractSaveAction;
38+
use Illuminate\Http\Request;
39+
40+
class SaveAndApprove extends AbstractSaveAction
41+
{
42+
protected ?int $order = 2;
43+
44+
public function getName(): string
45+
{
46+
return 'save_and_approve';
47+
}
48+
49+
public function getButtonText(): string
50+
{
51+
return trans('backpack::crud.save_action_save_and_approve');
52+
}
53+
54+
public function isVisible(CrudPanel $crud): bool
55+
{
56+
return $crud->hasAccess('update') && $crud->entry?->canBeApproved();
57+
}
58+
59+
public function getRedirectUrl(CrudPanel $crud, Request $request, $itemId = null): ?string
60+
{
61+
return route('admin.invoices.approve', $itemId ?? $request->input('id'));
62+
}
63+
}
64+
```
65+
66+
> **Tip:** `AbstractSaveAction` already implements `toArray()`, order handling, and sensible defaults. Override only what you need. If you must store additional data, you can still return a custom array by implementing `SaveActionInterface` yourself.
67+
68+
### Registering class-based actions
69+
70+
Inside your CrudController you can now pass the class instead of an array:
71+
72+
```php
73+
use App\Backpack\Crud\SaveActions\SaveAndApprove;
74+
75+
CRUD::replaceSaveActions([
76+
new SaveAndApprove(),
77+
\Backpack\CRUD\app\Library\CrudPanel\SaveActions\SaveAndBack::class,
78+
]);
79+
```
80+
81+
Backpack recognizes three inputs when registering save actions:
82+
- an instantiated save action class;
83+
- the fully qualified class name (it is resolved via the container so dependencies can be injected);
84+
- the legacy associative array definition (still supported).
85+
86+
The action `order` is taken from the class (or array) and Backpack reorders conflicts automatically. If you need to adjust the order later you can still call `CRUD::orderSaveActions()`.
87+
88+
### Using classes in Blade components
89+
90+
The `bp-dataform` component accept save action classes through the `:save-actions` attribute. This allows you to reuse the same custom buttons outside CrudControllers:
91+
92+
```php
93+
<x-bp-dataform
94+
controller="\App\Http\Controllers\Admin\InvoiceCrudController"
95+
:save-actions="[
96+
new App\\Backpack\\Crud\\SaveActions\\SaveAndApprove(),
97+
Backpack\\CRUD\\app\\Library\\CrudPanel\\SaveActions\\SaveAndBack::class,
98+
]"
99+
/>
100+
```
101+
102+
When no save actions are provided, Backpack falls back to the defaults registered on the controller.
103+
19104
<a name="save-actions-api"></a>
20105
## Save Actions API
21106

0 commit comments

Comments
 (0)