1 | <?php |
||
28 | final class TwigGridRenderer implements GridRendererInterface |
||
29 | { |
||
30 | /** |
||
31 | * @var \Twig_Environment |
||
32 | */ |
||
33 | private $twig; |
||
34 | |||
35 | /** |
||
36 | * @var ServiceRegistryInterface |
||
37 | */ |
||
38 | private $fieldsRegistry; |
||
39 | |||
40 | /** |
||
41 | * @var FormFactoryInterface |
||
42 | */ |
||
43 | private $formFactory; |
||
44 | |||
45 | /** |
||
46 | * @var FormTypeRegistryInterface |
||
47 | */ |
||
48 | private $formTypeRegistry; |
||
49 | |||
50 | /** |
||
51 | * @var string |
||
52 | */ |
||
53 | private $defaultTemplate; |
||
54 | |||
55 | /** |
||
56 | * @var array |
||
57 | */ |
||
58 | private $actionTemplates; |
||
59 | |||
60 | /** |
||
61 | * @var array |
||
62 | */ |
||
63 | private $filterTemplates; |
||
64 | |||
65 | /** |
||
66 | * @param \Twig_Environment $twig |
||
67 | * @param ServiceRegistryInterface $fieldsRegistry |
||
68 | * @param FormFactoryInterface $formFactory |
||
69 | * @param FormTypeRegistryInterface $formTypeRegistry |
||
70 | * @param string $defaultTemplate |
||
71 | * @param array $actionTemplates |
||
72 | * @param array $filterTemplates |
||
73 | */ |
||
74 | public function __construct( |
||
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | public function render(GridViewInterface $gridView, ?string $template = null) |
||
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | public function renderField(GridViewInterface $gridView, Field $field, $data) |
||
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | public function renderAction(GridViewInterface $gridView, Action $action, $data = null) |
||
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | public function renderFilter(GridViewInterface $gridView, Filter $filter) |
||
135 | { |
||
136 | $template = $this->getFilterTemplate($filter); |
||
137 | |||
138 | $form = $this->formFactory->createNamed('criteria', FormType::class, [], [ |
||
139 | 'allow_extra_fields' => true, |
||
140 | 'csrf_protection' => false, |
||
141 | 'required' => false, |
||
142 | ]); |
||
143 | $form->add( |
||
144 | $filter->getName(), |
||
145 | $this->formTypeRegistry->get($filter->getType(), 'default'), |
||
146 | $filter->getFormOptions() |
||
147 | ); |
||
148 | |||
149 | $criteria = $gridView->getParameters()->get('criteria', []); |
||
150 | $form->submit($criteria); |
||
151 | |||
152 | return $this->twig->render($template, [ |
||
153 | 'grid' => $gridView, |
||
154 | 'filter' => $filter, |
||
155 | 'form' => $form->get($filter->getName())->createView(), |
||
156 | ]); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @param Filter $filter |
||
161 | * |
||
162 | * @return string |
||
163 | * |
||
164 | * @throws \InvalidArgumentException |
||
165 | */ |
||
166 | private function getFilterTemplate(Filter $filter): string |
||
180 | } |
||
181 |