forked from Sylius/Stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwigBulkActionGridRenderer.php
More file actions
58 lines (50 loc) · 1.93 KB
/
TwigBulkActionGridRenderer.php
File metadata and controls
58 lines (50 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace App\Grid\Renderer;
use Sylius\Bundle\ResourceBundle\Grid\Parser\OptionsParserInterface;
use Sylius\Component\Grid\Definition\Action;
use Sylius\Component\Grid\Renderer\BulkActionGridRendererInterface;
use Sylius\Component\Grid\View\GridViewInterface;
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Twig\Environment;
#[AsDecorator(decorates: 'sylius.custom_bulk_action_grid_renderer.twig')]
final class TwigBulkActionGridRenderer implements BulkActionGridRendererInterface
{
public function __construct(
private Environment $twig,
private OptionsParserInterface $optionsParser,
#[Autowire(param: 'sylius.grid.templates.bulk_action')]
private array $bulkActionTemplates = [],
private readonly ?RequestStack $requestStack = null,
) {
}
public function renderBulkAction(GridViewInterface $gridView, Action $bulkAction, $data = null): string
{
$type = $bulkAction->getType();
if (!isset($this->bulkActionTemplates[$type])) {
throw new \InvalidArgumentException(sprintf('Missing template for bulk action type "%s".', $type));
}
$options = $this->optionsParser->parseOptions(
$bulkAction->getOptions(),
$this->requestStack?->getCurrentRequest() ?? new Request(),
$data,
);
return $this->twig->render($this->bulkActionTemplates[$type], [
'grid' => $gridView,
'action' => $bulkAction,
'data' => $data,
'options' => $options,
]);
}
}