Skip to content

Commit 6e74dde

Browse files
Add filters from grid storage on redirect handler (#1047)
| Q | A | --------------- | ----- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Related tickets | | License | MIT On Sylius, it uses grid filters on redirectToIndex action. https://github.com/Sylius/Sylius/blob/fb47637cef02d7573553b84315e1899f77221699/src/Sylius/Bundle/AdminBundle/Controller/RedirectHandler.php#L35 The new routing system has a new RedirectHandler service, so we need to implement this feature again. Test when redirecting the create product to update operation. ![image](https://github.com/user-attachments/assets/7fb2996c-eeb6-464a-9d59-4a4bb773bce7)
2 parents 2c3adea + 9ff7b87 commit 6e74dde

File tree

4 files changed

+65
-4
lines changed

4 files changed

+65
-4
lines changed

src/Bundle/Resources/config/services/routing.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
<argument type="service" id="router" />
129129
<argument type="service" id="sylius.expression_language.argument_parser.routing" />
130130
<argument type="service" id="sylius.routing.factory.operation_route_name_factory" />
131+
<argument type="service" id="sylius.grid.filter_storage" on-invalid="null" />
131132
</service>
132133
<service id="Sylius\Resource\Symfony\Routing\RedirectHandlerInterface" alias="sylius.routing.redirect_handler" />
133134
</services>

src/Component/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@
5454
"phpspec/phpspec": "^7.3",
5555
"phpspec/prophecy-phpunit": "^2.0",
5656
"phpunit/phpunit": "^9.5",
57-
"sylius/grid": "^1.7 || ^1.13@alpha",
57+
"sylius/grid": "^1.13",
58+
"sylius/grid-bundle": "^1.13",
5859
"symfony/serializer": "^6.4 || ^7.1",
5960
"symfony/workflow": "^6.4 || ^7.1",
6061
"twig/twig": "^3.0"

src/Component/spec/Symfony/Routing/RedirectHandlerSpec.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use PhpSpec\ObjectBehavior;
1717
use Prophecy\Argument;
18+
use Sylius\Bundle\GridBundle\Storage\FilterStorageInterface;
1819
use Sylius\Resource\Metadata\BulkUpdate;
1920
use Sylius\Resource\Metadata\Create;
2021
use Sylius\Resource\Metadata\Delete;
@@ -31,11 +32,13 @@ function let(
3132
RouterInterface $router,
3233
ArgumentParserInterface $argumentParser,
3334
OperationRouteNameFactoryInterface $operationRouteNameFactory,
35+
FilterStorageInterface $filterStorage,
3436
): void {
3537
$this->beConstructedWith(
3638
$router,
3739
$argumentParser,
3840
$operationRouteNameFactory,
41+
$filterStorage,
3942
);
4043
}
4144

@@ -48,12 +51,14 @@ function it_redirects_to_resource_with_id_argument_by_default(
4851
\stdClass $data,
4952
Request $request,
5053
RouterInterface $router,
54+
FilterStorageInterface $filterStorage,
5155
): void {
5256
$data->id = 'xyz';
5357
$operation = new Create(redirectToRoute: 'app_dummy_index');
5458
$resource = new ResourceMetadata(alias: 'app.book');
5559
$operation = $operation->withResource($resource);
5660

61+
$filterStorage->all()->willReturn([]);
5762
$router->generate('app_dummy_index', ['id' => 'xyz'])->willReturn('/dummies')->shouldBeCalled();
5863

5964
$this->redirectToResource($data, $operation, $request);
@@ -63,12 +68,14 @@ function it_redirects_to_resource_with_custom_identifier_argument_by_default(
6368
\stdClass $data,
6469
Request $request,
6570
RouterInterface $router,
71+
FilterStorageInterface $filterStorage,
6672
): void {
6773
$data->code = 'xyz';
6874
$operation = new Create(redirectToRoute: 'app_dummy_index');
6975
$resource = new ResourceMetadata(alias: 'app.ok', identifier: 'code');
7076
$operation = $operation->withResource($resource);
7177

78+
$filterStorage->all()->willReturn([]);
7279
$router->generate('app_dummy_index', ['code' => 'xyz'])->willReturn('/dummies')->shouldBeCalled();
7380

7481
$this->redirectToResource($data, $operation, $request);
@@ -77,12 +84,14 @@ function it_redirects_to_resource_with_custom_identifier_argument_by_default(
7784
function it_redirects_to_resource_with_id_via_property_access(
7885
Request $request,
7986
RouterInterface $router,
87+
FilterStorageInterface $filterStorage,
8088
): void {
8189
$data = new BoardGameResource('uid');
8290
$operation = new Create(redirectToRoute: 'app_board_game_index');
8391
$resource = new ResourceMetadata(alias: 'app.board_game');
8492
$operation = $operation->withResource($resource);
8593

94+
$filterStorage->all()->willReturn([]);
8695
$router->generate('app_board_game_index', ['id' => 'uid'])->willReturn('/board-games')->shouldBeCalled();
8796

8897
$this->redirectToResource($data, $operation, $request);
@@ -92,12 +101,14 @@ function it_redirects_to_resource_with_custom_arguments(
92101
\stdClass $data,
93102
Request $request,
94103
RouterInterface $router,
104+
FilterStorageInterface $filterStorage,
95105
): void {
96106
$data->code = 'xyz';
97107
$operation = new Create(redirectToRoute: 'app_dummy_index', redirectArguments: ['code' => 'resource.code']);
98108
$resource = new ResourceMetadata(alias: 'app.book');
99109
$operation = $operation->withResource($resource);
100110

111+
$filterStorage->all()->willReturn([]);
101112
$router->generate('app_dummy_index', ['code' => 'xyz'])->willReturn('/dummies')->shouldBeCalled();
102113

103114
$this->redirectToResource($data, $operation, $request);
@@ -107,6 +118,7 @@ function it_redirects_to_resource_with_id_via_the_getter(
107118
Request $request,
108119
RouterInterface $router,
109120
ArgumentParserInterface $argumentParser,
121+
FilterStorageInterface $filterStorage,
110122
): void {
111123
$data = new BoardGameResource('uid');
112124
$operation = new Create(redirectToRoute: 'app_board_game_index', redirectArguments: ['id' => 'resource.id()']);
@@ -115,6 +127,7 @@ function it_redirects_to_resource_with_id_via_the_getter(
115127

116128
$argumentParser->parseExpression('resource.id()', ['resource' => $data])->willReturn('uid');
117129

130+
$filterStorage->all()->willReturn([]);
118131
$router->generate('app_board_game_index', ['id' => 'uid'])->willReturn('/board-games')->shouldBeCalled();
119132

120133
$this->redirectToResource($data, $operation, $request);
@@ -124,27 +137,65 @@ function it_redirects_to_resource_without_arguments_after_delete_operation_by_de
124137
\stdClass $data,
125138
Request $request,
126139
RouterInterface $router,
140+
FilterStorageInterface $filterStorage,
127141
): void {
128142
$data->id = 'xyz';
129143
$operation = new Delete(redirectToRoute: 'app_dummy_index');
130144
$resource = new ResourceMetadata(alias: 'app.book');
131145
$operation = $operation->withResource($resource);
132146

147+
$filterStorage->all()->willReturn([]);
133148
$router->generate('app_dummy_index', [])->willReturn('/dummies')->shouldBeCalled();
134149

135150
$this->redirectToResource($data, $operation, $request);
136151
}
137152

153+
function it_uses_filters_from_grid_storage_when_redirecting_to_an_index_operation(
154+
\stdClass $data,
155+
Request $request,
156+
RouterInterface $router,
157+
FilterStorageInterface $filterStorage,
158+
): void {
159+
$data->id = 'xyz';
160+
$operation = new Delete(redirectToRoute: 'app_dummy_index');
161+
$resource = new ResourceMetadata(alias: 'app.book');
162+
$operation = $operation->withResource($resource);
163+
164+
$filterStorage->all()->willReturn(['criteria' => ['enabled' => true]]);
165+
$router->generate('app_dummy_index', ['criteria' => ['enabled' => true]])->willReturn('/dummies')->shouldBeCalled();
166+
167+
$this->redirectToResource($data, $operation, $request);
168+
}
169+
170+
function it_do_not_use_filters_from_grid_storage_when_redirecting_to_an_update_operation(
171+
\stdClass $data,
172+
Request $request,
173+
RouterInterface $router,
174+
FilterStorageInterface $filterStorage,
175+
): void {
176+
$data->id = 'xyz';
177+
$operation = new Create(redirectToRoute: 'app_dummy_update');
178+
$resource = new ResourceMetadata(alias: 'app.book');
179+
$operation = $operation->withResource($resource);
180+
181+
$filterStorage->all()->willReturn(['criteria' => ['enabled' => true]]);
182+
$router->generate('app_dummy_update', ['id' => 'xyz'])->willReturn('/dummies')->shouldBeCalled();
183+
184+
$this->redirectToResource($data, $operation, $request);
185+
}
186+
138187
function it_redirects_to_resource_without_arguments_after_bulk_operation_by_default(
139188
\stdClass $data,
140189
Request $request,
141190
RouterInterface $router,
191+
FilterStorageInterface $filterStorage,
142192
): void {
143193
$data->id = 'xyz';
144194
$operation = new BulkUpdate(redirectToRoute: 'app_dummy_index');
145195
$resource = new ResourceMetadata(alias: 'app.book');
146196
$operation = $operation->withResource($resource);
147197

198+
$filterStorage->all()->willReturn([]);
148199
$router->generate('app_dummy_index', [])->willReturn('/dummies')->shouldBeCalled();
149200

150201
$this->redirectToResource($data, $operation, $request);
@@ -153,7 +204,9 @@ function it_redirects_to_resource_without_arguments_after_bulk_operation_by_defa
153204
function it_redirects_to_route(
154205
\stdClass $data,
155206
RouterInterface $router,
207+
FilterStorageInterface $filterStorage,
156208
): void {
209+
$filterStorage->all()->willReturn([]);
157210
$router->generate('app_dummy_index', [])->willReturn('/dummies')->shouldBeCalled();
158211

159212
$this->redirectToRoute($data, 'app_dummy_index');

src/Component/src/Symfony/Routing/RedirectHandler.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Sylius\Resource\Symfony\Routing;
1515

16+
use Sylius\Bundle\GridBundle\Storage\FilterStorageInterface;
1617
use Sylius\Resource\Metadata\BulkOperationInterface;
1718
use Sylius\Resource\Metadata\DeleteOperationInterface;
1819
use Sylius\Resource\Metadata\HttpOperation;
@@ -33,6 +34,7 @@ public function __construct(
3334
private RouterInterface $router,
3435
private ArgumentParserInterface $argumentParser,
3536
private OperationRouteNameFactoryInterface $operationRouteNameFactory,
37+
private ?FilterStorageInterface $filterStorage = null,
3638
) {
3739
}
3840

@@ -44,7 +46,7 @@ public function redirectToResource(mixed $data, HttpOperation $operation, Reques
4446
throw new \RuntimeException(sprintf('Operation "%s" has no redirection route, but it should.', $operation->getName() ?? ''));
4547
}
4648

47-
$parameters = $this->getRouteArguments($data, $operation, $request);
49+
$parameters = $this->getRouteArguments($data, $operation);
4850

4951
return $this->redirectToRoute($data, $route, $parameters);
5052
}
@@ -53,17 +55,21 @@ public function redirectToOperation(mixed $data, HttpOperation $operation, Reque
5355
{
5456
$route = $this->operationRouteNameFactory->createRouteName($operation, $newOperation);
5557

56-
$parameters = $this->getRouteArguments($data, $operation, $request);
58+
$parameters = $this->getRouteArguments($data, $operation);
5759

5860
return $this->redirectToRoute($data, $route, $parameters);
5961
}
6062

6163
public function redirectToRoute(mixed $data, string $route, array $parameters = []): RedirectResponse
6264
{
65+
if (\str_ends_with($route, '_index')) {
66+
$parameters = array_merge($parameters, $this->filterStorage?->all() ?? []);
67+
}
68+
6369
return new RedirectResponse($this->router->generate($route, $parameters));
6470
}
6571

66-
private function getRouteArguments(mixed $data, HttpOperation $operation, Request $request): array
72+
private function getRouteArguments(mixed $data, HttpOperation $operation): array
6773
{
6874
$resource = $operation->getResource();
6975

0 commit comments

Comments
 (0)