Skip to content

Commit e069285

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

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.

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)