Skip to content

Commit 92e2a5e

Browse files
committed
Added support for row_attr and choice_attr
1 parent be4394f commit 92e2a5e

File tree

1 file changed

+48
-22
lines changed

1 file changed

+48
-22
lines changed

src/StimulusBundle/src/Form/Extension/FormTypeExtension.php

+48-22
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22

3-
declare(strict_types=1);
4-
53
/*
64
* This file is part of the Symfony package.
75
*
@@ -34,36 +32,64 @@ public static function getExtendedTypes(): iterable
3432
public function buildView(FormView $view, FormInterface $form, array $options): void
3533
{
3634
if (
37-
null === $options['stimulus_controller']
38-
&& null === $options['stimulus_target']
39-
&& null === $options['stimulus_action']
35+
isset($options['stimulus_controller'])
36+
|| !isset($options['stimulus_target'])
37+
|| !isset($options['stimulus_action'])
4038
) {
41-
return;
42-
}
39+
$this->stimulusAttributes = new StimulusAttributes(new Environment(new ArrayLoader()));
4340

44-
$this->stimulusAttributes = new StimulusAttributes(new Environment(new ArrayLoader()));
41+
if (isset($options['stimulus_controller'])) {
42+
$this->handleController($options['stimulus_controller']);
43+
}
4544

46-
if (true === \array_key_exists('stimulus_controller', $options)) {
47-
$this->handleController($options['stimulus_controller']);
48-
}
45+
if (isset($options['stimulus_target'])) {
46+
$this->handleTarget($options['stimulus_target']);
47+
}
4948

50-
if (true === \array_key_exists('stimulus_target', $options)) {
51-
$this->handleTarget($options['stimulus_target']);
52-
}
49+
if (isset($options['stimulus_action'])) {
50+
$this->handleAction($options['stimulus_action']);
51+
}
5352

54-
if (true === \array_key_exists('stimulus_action', $options)) {
55-
$this->handleAction($options['stimulus_action']);
53+
$attributes = array_merge($view->vars['attr'], $this->stimulusAttributes->toArray());
54+
$view->vars['attr'] = $attributes;
5655
}
5756

58-
$attributes = array_merge($view->vars['attr'], $this->stimulusAttributes->toArray());
57+
foreach (['row_attr', 'choice_attr'] as $index) {
58+
if (
59+
isset($options[$index])
60+
&& (
61+
isset($options[$index]['stimulus_controller'])
62+
|| isset($options[$index]['stimulus_target'])
63+
|| isset($options[$index]['stimulus_action'])
64+
)
65+
) {
66+
$this->stimulusAttributes = new StimulusAttributes(new Environment(new ArrayLoader()));
67+
68+
if (isset($options[$index]['stimulus_controller'])) {
69+
$this->handleController($options[$index]['stimulus_controller']);
70+
unset($options[$index]['stimulus_controller']);
71+
}
72+
73+
if (isset($options[$index]['stimulus_target'])) {
74+
$this->handleTarget($options[$index]['stimulus_target']);
75+
unset($options[$index]['stimulus_target']);
76+
}
5977

60-
$view->vars['attr'] = $attributes;
78+
if (isset($options[$index]['stimulus_action'])) {
79+
$this->handleAction($options[$index]['stimulus_action']);
80+
unset($options[$index]['stimulus_action']);
81+
}
82+
83+
$attributes = array_merge($options[$index], $this->stimulusAttributes->toArray());
84+
$view->vars[$index] = $attributes;
85+
}
86+
}
6187
}
6288

6389
private function handleController(string|array $controllers): void
6490
{
6591
if (\is_string($controllers)) {
66-
$controllers = [$controllcers];
92+
$controllers = [$controllers];
6793
}
6894

6995
foreach ($controllers as $controllerName => $controller) {
@@ -129,8 +155,8 @@ public function configureOptions(OptionsResolver $resolver): void
129155
'stimulus_target' => null,
130156
]);
131157

132-
$resolver->setAllowedTypes('stimulus_action', ['string', 'array', 'null']);
133-
$resolver->setAllowedTypes('stimulus_controller', ['string', 'array', 'null']);
134-
$resolver->setAllowedTypes('stimulus_target', ['string', 'array', 'null']);
158+
$resolver->setAllowedTypes('stimulus_action', ['string', 'array', 'callable', 'null']);
159+
$resolver->setAllowedTypes('stimulus_controller', ['string', 'array', 'callable', 'null']);
160+
$resolver->setAllowedTypes('stimulus_target', ['string', 'array', 'callable', 'null']);
135161
}
136162
}

0 commit comments

Comments
 (0)