Skip to content

Commit fcdbae5

Browse files
committed
Allow to define a template in action options
1 parent e33f7a6 commit fcdbae5

3 files changed

Lines changed: 38 additions & 3 deletions

File tree

src/Bundle/Grid/Renderer/TwigGridRenderer.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ public function renderAction(GridViewInterface $gridView, Action $action, $data
6767
}
6868

6969
$type = $action->getType();
70-
if (!isset($this->actionTemplates[$type])) {
70+
$template = $action->getOptions()['template'] ?? $this->actionTemplates[$type] ?? null;
71+
72+
if (null === $template) {
7173
throw new \InvalidArgumentException(sprintf('Missing template for action type "%s".', $type));
7274
}
7375

@@ -77,7 +79,7 @@ public function renderAction(GridViewInterface $gridView, Action $action, $data
7779
$data,
7880
);
7981

80-
return $this->twig->render($this->actionTemplates[$type], [
82+
return $this->twig->render($template, [
8183
'grid' => $gridView,
8284
'action' => $action,
8385
'data' => $data,

src/Bundle/spec/Grid/Renderer/TwigGridRendererSpec.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,41 @@ function it_uses_twig_to_render_the_action(
7777
$this->renderAction($gridView, $action)->shouldReturn('<a href="#">Action!</a>');
7878
}
7979

80+
function it_can_use_the_template_defined_in_the_action_options(
81+
Environment $twig,
82+
OptionsParserInterface $optionsParser,
83+
ResourceGridView $gridView,
84+
Action $action,
85+
RequestConfiguration $requestConfiguration,
86+
Request $request,
87+
): void {
88+
$action->getType()->willReturn('custom');
89+
$action->getOptions()->willReturn(['template' => 'path/to/custom/template']);
90+
91+
$gridView->getRequestConfiguration()->willReturn($requestConfiguration);
92+
$requestConfiguration->getRequest()->willReturn($request);
93+
94+
$optionsParser->parseOptions(['template' => 'path/to/custom/template'], $request, null)->shouldBeCalled();
95+
96+
$twig
97+
->render('path/to/custom/template', [
98+
'grid' => $gridView,
99+
'action' => $action,
100+
'data' => null,
101+
'options' => [],
102+
])
103+
->willReturn('<a href="#">Action!</a>')
104+
->shouldBeCalled()
105+
;
106+
107+
$this->renderAction($gridView, $action)->shouldReturn('<a href="#">Action!</a>');
108+
}
109+
80110
function it_throws_an_exception_if_template_is_not_configured_for_given_action_type(
81111
ResourceGridView $gridView,
82112
Action $action,
83113
): void {
114+
$action->getOptions()->willReturn([]);
84115
$action->getType()->willReturn('foo');
85116

86117
$this

tests/Application/src/Subscription/Grid/SubscriptionGrid.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ public function buildGrid(GridBuilderInterface $gridBuilder): void
5151
)
5252
->addActionGroup(
5353
ItemActionGroup::create(
54-
ShowAction::create(),
54+
Action::create('show', 'custom_show')
55+
->setOptions(['template' => 'grid/action/show.html.twig'])
56+
,
5557
UpdateAction::create(),
5658
DeleteAction::create(),
5759
Action::create('accept', 'apply_transition')

0 commit comments

Comments
 (0)