|
1 | 1 | <?php
|
2 | 2 |
|
3 |
| -declare(strict_types=1); |
4 |
| - |
5 | 3 | /*
|
6 | 4 | * This file is part of the Symfony package.
|
7 | 5 | *
|
@@ -34,36 +32,64 @@ public static function getExtendedTypes(): iterable
|
34 | 32 | public function buildView(FormView $view, FormInterface $form, array $options): void
|
35 | 33 | {
|
36 | 34 | 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']) |
40 | 38 | ) {
|
41 |
| - return; |
42 |
| - } |
| 39 | + $this->stimulusAttributes = new StimulusAttributes(new Environment(new ArrayLoader())); |
43 | 40 |
|
44 |
| - $this->stimulusAttributes = new StimulusAttributes(new Environment(new ArrayLoader())); |
| 41 | + if (isset($options['stimulus_controller'])) { |
| 42 | + $this->handleController($options['stimulus_controller']); |
| 43 | + } |
45 | 44 |
|
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 | + } |
49 | 48 |
|
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 | + } |
53 | 52 |
|
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; |
56 | 55 | }
|
57 | 56 |
|
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 | + } |
59 | 77 |
|
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 | + } |
61 | 87 | }
|
62 | 88 |
|
63 | 89 | private function handleController(string|array $controllers): void
|
64 | 90 | {
|
65 | 91 | if (\is_string($controllers)) {
|
66 |
| - $controllers = [$controllcers]; |
| 92 | + $controllers = [$controllers]; |
67 | 93 | }
|
68 | 94 |
|
69 | 95 | foreach ($controllers as $controllerName => $controller) {
|
@@ -129,8 +155,8 @@ public function configureOptions(OptionsResolver $resolver): void
|
129 | 155 | 'stimulus_target' => null,
|
130 | 156 | ]);
|
131 | 157 |
|
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']); |
135 | 161 | }
|
136 | 162 | }
|
0 commit comments