Skip to content

Commit d05bb33

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

File tree

12 files changed

+83
-8
lines changed

12 files changed

+83
-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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 App\State\Responder;
15+
16+
use App\Entity\Speaker;
17+
use Sylius\Resource\Context\Context;
18+
use Sylius\Resource\Metadata\Operation;
19+
use Sylius\Resource\State\ResponderInterface;
20+
use Symfony\Component\HttpFoundation\RedirectResponse;
21+
use Symfony\Component\Routing\RouterInterface;
22+
use Webmozart\Assert\Assert;
23+
24+
final class RedirectToSpeakerTalksResponder implements ResponderInterface
25+
{
26+
public function __construct(
27+
private readonly RouterInterface $router,
28+
) {
29+
}
30+
31+
/**
32+
* @param Speaker[] $data
33+
*/
34+
public function respond(mixed $data, Operation $operation, Context $context): RedirectResponse
35+
{
36+
Assert::allIsInstanceOf($data, Speaker::class);
37+
38+
$ids = array_map(fn (Speaker $speaker) => $speaker->getId(), $data);
39+
40+
$path = $this->router->generate('app_admin_talk_index', [
41+
'criteria' => [
42+
'speaker' => $ids,
43+
],
44+
]);
45+
46+
return new RedirectResponse($path);
47+
}
48+
}

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.

src/BootstrapAdminUi/templates/shared/crud/index/content/grid/data_table.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<div class="card-body border-bottom py-3">
1111
<div class="d-flex">
1212
{% if data|length > 0 and definition.actionGroups.bulk is defined and definition.getEnabledActions('bulk')|length > 0 %}
13-
<div class="sylius-grid-nav__bulk">
13+
<div class="sylius-grid-nav__bulk grid">
1414
{% for action in definition.getEnabledActions('bulk') %}
1515
{{ sylius_grid_render_bulk_action(resources, action, null) }}
1616
{% endfor %}

src/BootstrapAdminUi/templates/shared/grid/bulk_action/delete.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{% set path = options.link.url|default(path(options.link.route|default(grid.requestConfiguration.getRouteName('bulk_delete')), options.link.parameters|default({}))) %}
22

3-
{% component 'sylius_bootstrap_admin_ui:delete_modal' with {id: 'bulk_delete', path: path, type: 'bulk-delete', form_attr: 'data-bulk-delete=index'} %}
3+
{% component 'sylius_bootstrap_admin_ui:delete_modal' with {id: 'bulk_delete', path: path, type: 'bulk-delete', form_attr: 'data-bulk-action=index'} %}
44
{% import '@SyliusBootstrapAdminUi/shared/helper/button.html.twig' as button %}
55

66
{% block trigger %}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{% import '@SyliusBootstrapAdminUi/shared/helper/button.html.twig' as button %}
2+
3+
{% set label = action.label|trans %}
4+
{% set path = path('app_admin_speaker_redirect_to_speaker_talks') %}
5+
{% set form_attr = 'data-bulk-action=index' %}
6+
7+
<form action="{{ path }}" method="GET" {{ form_attr }}>
8+
{{ button.default({
9+
text: label|trans,
10+
type: 'submit',
11+
class: 'btn-ghost-primary',
12+
disabled: true,
13+
attr: 'data-check-all-action=index',
14+
icon: 'tabler:list-letters'
15+
}) }}
16+
</form>

0 commit comments

Comments
 (0)