Skip to content

Commit 124d493

Browse files
committed
PoC view talks filter by multiple speakers on a bulk action of the grid
1 parent eb40e44 commit 124d493

File tree

13 files changed

+87
-8
lines changed

13 files changed

+87
-8
lines changed

app/Entity/File.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function getPath(): ?string
4141
return $this->path;
4242
}
4343

44-
public function setPath(string $path): void
44+
public function setPath(?string $path): void
4545
{
4646
$this->path = $path;
4747
}

app/Entity/Speaker.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use App\Form\SpeakerType;
1717
use App\Grid\SpeakerGrid;
1818
use App\Repository\SpeakerRepository;
19+
use App\State\Responder\RedirectToSpeakerTalksResponder;
1920
use Doctrine\Common\Collections\ArrayCollection;
2021
use Doctrine\Common\Collections\Collection;
2122
use Doctrine\ORM\Mapping as ORM;
@@ -38,6 +39,11 @@
3839
new Create(),
3940
new Update(),
4041
new Index(grid: SpeakerGrid::class),
42+
new Index(
43+
shortName: 'redirect_to_speaker_talks',
44+
responder: RedirectToSpeakerTalksResponder::class,
45+
repositoryMethod: 'findById',
46+
),
4147
new Delete(),
4248
new BulkDelete(),
4349
],

app/Grid/SpeakerGrid.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ public function buildGrid(GridBuilderInterface $gridBuilder): void
8989
->addActionGroup(
9090
BulkActionGroup::create(
9191
DeleteAction::create(),
92+
Action::create('redirect_to_speaker_talks', 'redirect_to_speaker_talks')
93+
->setLabel('View talks'),
9294
),
9395
)
9496
;

app/Grid/TalkGrid.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public function buildGrid(GridBuilderInterface $gridBuilder): void
5555
->addFilter(
5656
Filter::create(name: 'speaker', type: SpeakerFilter::class)
5757
->setLabel('app.ui.speaker')
58+
->setFormOptions(['multiple' => true])
5859
->setOptions(['fields' => ['speakers.id']]),
5960
)
6061
->addFilter(
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace App\State\Processor;
4+
5+
use Sylius\Resource\Context\Context;
6+
use Sylius\Resource\Metadata\Operation;
7+
use Sylius\Resource\State\ProcessorInterface;
8+
9+
final class UpdateSpeakerProcessor implements ProcessorInterface
10+
{
11+
public function process(mixed $data, Operation $operation, Context $context): mixed
12+
{
13+
dd($data);
14+
}
15+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace App\State\Responder;
4+
5+
use App\Entity\Speaker;
6+
use Sylius\Resource\Context\Context;
7+
use Sylius\Resource\Metadata\Operation;
8+
use Sylius\Resource\State\ResponderInterface;
9+
use Symfony\Component\HttpFoundation\RedirectResponse;
10+
use Symfony\Component\Routing\RouterInterface;
11+
use Webmozart\Assert\Assert;
12+
13+
final class RedirectToSpeakerTalksResponder implements ResponderInterface
14+
{
15+
public function __construct(
16+
private readonly RouterInterface $router,
17+
) {
18+
}
19+
20+
/**
21+
* @param Speaker[] $data
22+
*/
23+
public function respond(mixed $data, Operation $operation, Context $context): RedirectResponse
24+
{
25+
Assert::allIsInstanceOf($data, Speaker::class);
26+
27+
$ids = array_map(fn (Speaker $speaker) => $speaker->getId(), $data);
28+
29+
$path = $this->router->generate('app_admin_talk_index', [
30+
'criteria' => [
31+
'speaker' => $ids,
32+
]
33+
]);
34+
35+
return new RedirectResponse($path);
36+
}
37+
}

config/packages/sylius_grid.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ sylius_grid:
22
templates:
33
filter:
44
'App\Grid\Filter\SpeakerFilter': '@SyliusBootstrapAdminUi/shared/grid/filter/entity.html.twig'
5+
bulk_action:
6+
'redirect_to_speaker_talks': 'shared/grid/bulk_action/redirect_to_speaker_talks.html.twig'

src/BootstrapAdminUi/assets/entrypoint.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import './styles/main.scss';
1111

12-
import './scripts/bulk-delete';
12+
import './scripts/bulk-action';
1313
import './scripts/check-all';
1414
import './scripts/menu-search';
1515

src/BootstrapAdminUi/assets/scripts/bulk-delete.js renamed to src/BootstrapAdminUi/assets/scripts/bulk-action.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
/* global document */
1111

12-
function syliusBulkDelete(form) {
13-
const groupName = form.getAttribute('data-bulk-delete');
12+
function syliusBulkAction(form) {
13+
const groupName = form.getAttribute('data-bulk-action');
1414
const groupItems = Array.from(document.querySelectorAll(`input[data-check-all-group="${groupName}"]`));
1515

1616
form.addEventListener('submit', (e) => {
@@ -31,5 +31,5 @@ function syliusBulkDelete(form) {
3131
}
3232

3333
(function () {
34-
document.querySelectorAll('[data-bulk-delete]').forEach(syliusBulkDelete);
34+
document.querySelectorAll('[data-bulk-action]').forEach(syliusBulkAction);
3535
}());

src/BootstrapAdminUi/public/app.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)