Skip to content

Commit 77a7152

Browse files
committed
[Docs] Improve actions' chapter
1 parent 5282de8 commit 77a7152

File tree

3 files changed

+210
-23
lines changed

3 files changed

+210
-23
lines changed

app/Grid/SpeakerGrid.php

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,38 +26,31 @@
2626
use Sylius\Bundle\GridBundle\Builder\Filter\StringFilter;
2727
use Sylius\Bundle\GridBundle\Builder\GridBuilderInterface;
2828
use Sylius\Bundle\GridBundle\Grid\AbstractGrid;
29-
use Sylius\Bundle\GridBundle\Grid\ResourceAwareGridInterface;
29+
use Sylius\Component\Grid\Attribute\AsGrid;
3030

31-
final class SpeakerGrid extends AbstractGrid implements ResourceAwareGridInterface
31+
#[AsGrid(
32+
resourceClass: Speaker::class,
33+
name: 'app_speaker',
34+
)]
35+
final class SpeakerGrid extends AbstractGrid
3236
{
33-
public static function getName(): string
34-
{
35-
return 'app_speaker';
36-
}
37-
38-
public function buildGrid(GridBuilderInterface $gridBuilder): void
37+
public function __invoke(GridBuilderInterface $gridBuilder): void
3938
{
4039
$gridBuilder
41-
->addFilter(
40+
->addOrderBy('firstName', 'asc')
41+
->withFilters(
4242
StringFilter::create('search', ['firstName', 'lastName', 'companyName'])
4343
->setLabel('sylius.ui.search'),
4444
)
45-
->addOrderBy('firstName', 'asc')
46-
->addField(
45+
->withFields(
4746
TwigField::create('avatar', 'speaker/grid/field/image.html.twig')
4847
->setPath('.'),
49-
)
50-
->addField(
5148
StringField::create('firstName')
5249
->setLabel('app.ui.first_name')
5350
->setSortable(true),
54-
)
55-
->addField(
5651
StringField::create('lastName')
5752
->setLabel('app.ui.last_name')
5853
->setSortable(true),
59-
)
60-
->addField(
6154
StringField::create('companyName')
6255
->setLabel('app.ui.company_name')
6356
->setSortable(true),
@@ -93,9 +86,4 @@ public function buildGrid(GridBuilderInterface $gridBuilder): void
9386
)
9487
;
9588
}
96-
97-
public function getResourceClass(): string
98-
{
99-
return Speaker::class;
100-
}
10189
}
213 KB
Loading

docs/grid/actions.md

Lines changed: 200 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,183 @@
11
# Actions
22

3+
## Action groups
4+
5+
<div data-full-width="false">
6+
7+
<figure><img src="../.gitbook/assets/action-groups.png" alt="Action groups"></figure>
8+
9+
</div>
10+
11+
Actions are classified into four types:
12+
13+
- main
14+
- item
15+
- subitem
16+
- bulk
17+
18+
## Built-in actions
19+
20+
The grid package provides the following built-in actions:
21+
22+
| Name | Usage |
23+
|------------------|------------|
24+
| create | main |
25+
| update | item, bulk |
26+
| delete | item, bulk |
27+
| show | item |
28+
| apply_transition | item, bulk |
29+
30+
### Create
31+
32+
```php
33+
<?php
34+
35+
declare(strict_types=1);
36+
37+
namespace App\Grid;
38+
39+
use App\Entity\Speaker;
40+
use Sylius\Bundle\GridBundle\Builder\Action\CreateAction;
41+
use Sylius\Bundle\GridBundle\Builder\ActionGroup\MainActionGroup;
42+
use Sylius\Bundle\GridBundle\Builder\GridBuilderInterface;
43+
use Sylius\Bundle\GridBundle\Grid\AbstractGrid;
44+
use Sylius\Component\Grid\Attribute\AsGrid;
45+
46+
#[AsGrid(
47+
resourceClass: Speaker::class,
48+
name: 'app_speaker',
49+
)]
50+
final class SpeakerGrid extends AbstractGrid
51+
{
52+
public function __invoke(GridBuilderInterface $gridBuilder): void
53+
{
54+
$gridBuilder
55+
->addActionGroup(
56+
MainActionGroup::create(
57+
// Add the "create" action into the "main" action group
58+
CreateAction::create(),
59+
),
60+
)
61+
;
62+
}
63+
}
64+
```
65+
66+
### Update
67+
68+
```php
69+
<?php
70+
71+
declare(strict_types=1);
72+
73+
namespace App\Grid;
74+
75+
use App\Entity\Speaker;
76+
use Sylius\Bundle\GridBundle\Builder\Action\UpdateAction;
77+
use Sylius\Bundle\GridBundle\Builder\ActionGroup\ItemActionGroup;
78+
use Sylius\Bundle\GridBundle\Builder\GridBuilderInterface;
79+
use Sylius\Bundle\GridBundle\Grid\AbstractGrid;
80+
use Sylius\Component\Grid\Attribute\AsGrid;
81+
82+
#[AsGrid(
83+
resourceClass: Speaker::class,
84+
name: 'app_speaker',
85+
)]
86+
final class SpeakerGrid extends AbstractGrid
87+
{
88+
public function __invoke(GridBuilderInterface $gridBuilder): void
89+
{
90+
$gridBuilder
91+
->addActionGroup(
92+
ItemActionGroup::create(
93+
// Add the "update" action into the "item" action group
94+
UpdateAction::create(),
95+
),
96+
)
97+
;
98+
}
99+
}
100+
```
101+
102+
### Delete
103+
104+
```php
105+
<?php
106+
107+
declare(strict_types=1);
108+
109+
namespace App\Grid;
110+
111+
use App\Entity\Speaker;
112+
use Sylius\Bundle\GridBundle\Builder\Action\DeleteAction;
113+
use Sylius\Bundle\GridBundle\Builder\ActionGroup\BulkActionGroup;
114+
use Sylius\Bundle\GridBundle\Builder\ActionGroup\ItemActionGroup;
115+
use Sylius\Bundle\GridBundle\Builder\GridBuilderInterface;
116+
use Sylius\Bundle\GridBundle\Grid\AbstractGrid;
117+
use Sylius\Component\Grid\Attribute\AsGrid;
118+
119+
#[AsGrid(
120+
resourceClass: Speaker::class,
121+
name: 'app_speaker',
122+
)]
123+
final class SpeakerGrid extends AbstractGrid
124+
{
125+
public function __invoke(GridBuilderInterface $gridBuilder): void
126+
{
127+
$gridBuilder
128+
->addActionGroup(
129+
ItemActionGroup::create(
130+
// Add the "delete" action into the "item" action group
131+
DeleteAction::create(),
132+
),
133+
)
134+
->addActionGroup(
135+
BulkActionGroup::create(
136+
// Add the "delete" action into the "bulk" action group
137+
DeleteAction::create(),
138+
),
139+
)
140+
;
141+
}
142+
}
143+
```
144+
145+
### Show
146+
147+
```php
148+
<?php
149+
150+
declare(strict_types=1);
151+
152+
namespace App\Grid;
153+
154+
use App\Entity\Speaker;
155+
use Sylius\Bundle\GridBundle\Builder\Action\ShowAction;
156+
use Sylius\Bundle\GridBundle\Builder\ActionGroup\ItemActionGroup;
157+
use Sylius\Bundle\GridBundle\Builder\GridBuilderInterface;
158+
use Sylius\Bundle\GridBundle\Grid\AbstractGrid;
159+
use Sylius\Component\Grid\Attribute\AsGrid;
160+
161+
#[AsGrid(
162+
resourceClass: Speaker::class,
163+
name: 'app_speaker',
164+
)]
165+
final class SpeakerGrid extends AbstractGrid
166+
{
167+
public function __invoke(GridBuilderInterface $gridBuilder): void
168+
{
169+
$gridBuilder
170+
->addActionGroup(
171+
ItemActionGroup::create(
172+
// Add the "show" action into the "item" action group
173+
ShowAction::create(),
174+
),
175+
)
176+
;
177+
}
178+
}
179+
```
180+
3181
## Creating a custom Action
4182

5183
There are certain cases when built-in action types are not enough.
@@ -11,25 +189,29 @@ In this example, we will specify the action button's icon to be `mail` and its
11189
colour to be `purple` inside the template.
12190

13191
{% code title="@App/Grid/Action/contactSupplier.html.twig" lineNumbers="true" %}
192+
14193
```twig
15194
{% import '@SyliusUi/Macro/buttons.html.twig' as buttons %}
16195
17196
{% set path = options.link.url|default(path(options.link.route, options.link.parameters)) %}
18197
19198
{{ buttons.default(path, action.label, null, 'mail', 'purple') }}
20199
```
200+
21201
{% endcode %}
22202

23203
Now configure the new action's template like below in
24204
`config/packages/sylius_grid.yaml`:
25205

26206
{% code title="config/packages/sylius_grid.yaml" lineNumbers="true" %}
207+
27208
```yaml
28209
sylius_grid:
29210
templates:
30211
action:
31212
contactSupplier: "@App/Grid/Action/contactSupplier.html.twig"
32213
```
214+
33215
{% endcode %}
34216
35217
From now on, you can use your new action type in the grid configuration!
@@ -40,6 +222,7 @@ suppliers, then you can configure the grid action:
40222
{% tabs %}
41223
{% tab title="YAML" %}
42224
{% code title="config/packages/sylius_grid.yaml" lineNumbers="true" %}
225+
43226
```yaml
44227
sylius_grid:
45228
grids:
@@ -59,11 +242,13 @@ sylius_grid:
59242
parameters:
60243
id: resource.id
61244
```
245+
62246
{% endcode %}
63247
{% endtab %}
64248
65249
{% tab title="PHP" %}
66250
{% code title="config/packages/sylius_grid.php" lineNumbers="true" %}
251+
67252
```php
68253
<?php
69254

@@ -92,11 +277,13 @@ return static function (GridConfig $grid): void {
92277
)
93278
};
94279
```
280+
95281
{% endcode %}
96282

97283
OR
98284

99285
{% code title="src/Grid/AdminSupplierGrid.php" lineNumbers="true" %}
286+
100287
```php
101288
<?php
102289

@@ -144,39 +331,45 @@ final class AdminSupplierGrid extends AbstractGrid implements ResourceAwareGridI
144331
}
145332
}
146333
```
334+
147335
{% endcode %}
148336
{% endtab %}
149337
{% endtabs %}
150338

151339
## Creating a custom Bulk Action
152340

153341
In some cases, forcing the user to click a button for each item in a grid isn't practical.
154-
Fortunately, you can take advantage of built-in bulk actions. However, these may not always be sufficient and might need customization.
342+
Fortunately, you can take advantage of built-in bulk actions. However, these may not always be sufficient and might need
343+
customization.
155344

156345
To do this, simply create your own bulk action template and register it inside the `sylius_grid`.
157346

158347
In the template we will specify the button's icon to be `export` and its
159348
colour to be `orange`.
160349

161350
{% code title="@App/Grid/BulkAction/export.html.twig" %}
351+
162352
```twig
163353
{% import '@SyliusUi/Macro/buttons.html.twig' as buttons %}
164354
165355
{% set path = options.link.url|default(path(options.link.route)) %}
166356
167357
{{ buttons.default(path, action.label, null, 'export', 'orange') }}
168358
```
359+
169360
{% endcode %}
170361

171362
Now configure the new action's template:
172363

173364
{% code title="config/packages/sylius_grid.yaml" lineNumbers="true" %}
365+
174366
```yaml
175367
sylius_grid:
176368
templates:
177369
bulk_action:
178370
export: "@App/Grid/BulkAction/export.html.twig"
179371
```
372+
180373
{% endcode %}
181374
182375
From now on, you can use your new bulk action type in the grid configuration!
@@ -187,6 +380,7 @@ ids. Now, you can configure the grid action:
187380
{% tabs %}
188381
{% tab title="YAML" %}
189382
{% code title="config/packages/sylius_grid.yaml" lineNumbers="true" %}
383+
190384
```yaml
191385
sylius_grid:
192386
grids:
@@ -203,11 +397,13 @@ sylius_grid:
203397
parameters:
204398
format: csv
205399
```
400+
206401
{% endcode %}
207402
{% endtab %}
208403
209404
{% tab title="PHP" %}
210405
{% code title="config/packages/sylius_grid.php" lineNumbers="true" %}
406+
211407
```php
212408
<?php
213409

@@ -237,11 +433,13 @@ return static function (GridConfig $grid) {
237433
)
238434
};
239435
```
436+
240437
{% endcode %}
241438

242439
OR
243440

244441
{% code title="src/Grid/AdminProductGrid.php" lineNumbers="true" %}
442+
245443
```php
246444
<?php
247445

@@ -289,6 +487,7 @@ final class AdminProductGrid extends AbstractGrid implements ResourceAwareGridIn
289487
}
290488
}
291489
```
490+
292491
{% endcode %}
293492
{% endtab %}
294493
{% endtabs %}

0 commit comments

Comments
 (0)