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
5183There 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
11189colour 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
23203Now 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
28209sylius_grid :
29210 templates :
30211 action :
31212 contactSupplier : " @App/Grid/Action/contactSupplier.html.twig"
32213` ` `
214+
33215{% endcode %}
34216
35217From 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
44227sylius_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
97283OR
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
153341In 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
156345To do this, simply create your own bulk action template and register it inside the ` sylius_grid ` .
157346
158347In the template we will specify the button's icon to be ` export ` and its
159348colour 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
171362Now configure the new action's template:
172363
173364{% code title="config/packages/sylius_grid.yaml" lineNumbers="true" %}
365+
174366``` yaml
175367sylius_grid :
176368 templates :
177369 bulk_action :
178370 export : " @App/Grid/BulkAction/export.html.twig"
179371` ` `
372+
180373{% endcode %}
181374
182375From 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
191385sylius_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
242439OR
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