Skip to content

Commit eefd674

Browse files
committed
Allow to define a template in action options
1 parent 574bbc2 commit eefd674

File tree

5 files changed

+95
-2
lines changed

5 files changed

+95
-2
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\Bundle\GridBundle\Builder\Action;
15+
16+
final class TwigAction
17+
{
18+
public static function create(
19+
string $name,
20+
string $template,
21+
array $options = [],
22+
): ActionInterface {
23+
$action = Action::create($name, $name);
24+
25+
$options = array_merge($options, ['template' => $template]);
26+
$action->setOptions($options);
27+
28+
return $action;
29+
}
30+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace spec\Sylius\Bundle\GridBundle\Builder\Action;
15+
16+
use PhpSpec\ObjectBehavior;
17+
use Sylius\Bundle\GridBundle\Builder\Action\ActionInterface;
18+
use Sylius\Bundle\GridBundle\Builder\Action\TwigAction;
19+
20+
final class TwigActionSpec extends ObjectBehavior
21+
{
22+
function it_is_initializable(): void
23+
{
24+
$this->shouldHaveType(TwigAction::class);
25+
}
26+
27+
function it_builds_create_actions(): void
28+
{
29+
$action = $this::create(name: 'dummy', template: 'path/to/dummy/template');
30+
31+
$action->shouldHaveType(ActionInterface::class);
32+
$action->toArray()->shouldReturn([
33+
'type' => 'dummy',
34+
'options' => [
35+
'template' => 'path/to/dummy/template',
36+
],
37+
]);
38+
}
39+
40+
function it_builds_create_action_with_options(): void
41+
{
42+
$action = $this::create(name: 'dummy', template: 'path/to/dummy/template', options: ['custom' => true]);
43+
44+
$action->shouldHaveType(ActionInterface::class);
45+
$action->toArray()->shouldReturn([
46+
'type' => 'dummy',
47+
'options' => [
48+
'custom' => true,
49+
'template' => 'path/to/dummy/template',
50+
],
51+
]);
52+
}
53+
}

tests/Application/config/sylius/grids.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ sylius_grid:
5858
actions:
5959
item:
6060
show:
61-
type: show
61+
type: custom_show
62+
options:
63+
template: 'grid/action/show.html.twig'
6264
limits: [10, 5, 15]
6365

6466
app_author:

tests/Application/config/sylius/grids/book.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use App\Entity\Book;
1616
use App\Grid\Builder\AttributeNationalityFilter;
1717
use App\Grid\Builder\NationalityFilter;
18+
use Sylius\Bundle\GridBundle\Builder\Action\TwigAction;
19+
use Sylius\Bundle\GridBundle\Builder\ActionGroup\ItemActionGroup;
1820
use Sylius\Bundle\GridBundle\Builder\Field\CallableField;
1921
use Sylius\Bundle\GridBundle\Builder\Field\StringField;
2022
use Sylius\Bundle\GridBundle\Builder\Filter\EntityFilter;
@@ -77,6 +79,11 @@
7779
->setSortable(true, 'price.currencyCode')
7880
->setOption('vars', ['th_class' => 'text-end']),
7981
)
82+
->addActionGroup(
83+
ItemActionGroup::create(
84+
TwigAction::create('custom_show', 'grid/action/show.html.twig'),
85+
),
86+
)
8087
->setLimits([10, 5, 15]),
8188
);
8289
};

tests/Application/src/Grid/BookGrid.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Sylius\Bundle\GridBundle\Builder\Action\CreateAction;
2020
use Sylius\Bundle\GridBundle\Builder\Action\DeleteAction;
2121
use Sylius\Bundle\GridBundle\Builder\Action\ShowAction;
22+
use Sylius\Bundle\GridBundle\Builder\Action\TwigAction;
2223
use Sylius\Bundle\GridBundle\Builder\Action\UpdateAction;
2324
use Sylius\Bundle\GridBundle\Builder\ActionGroup\ItemActionGroup;
2425
use Sylius\Bundle\GridBundle\Builder\ActionGroup\MainActionGroup;
@@ -98,7 +99,7 @@ public function buildGrid(GridBuilderInterface $gridBuilder): void
9899
)
99100
->addActionGroup(
100101
ItemActionGroup::create(
101-
ShowAction::create([
102+
TwigAction::create(name: 'custom_show', template: 'grid/action/show.html.twig', options: [
102103
'link' => [
103104
'route' => 'app_book_show',
104105
],

0 commit comments

Comments
 (0)