diff --git a/src/StimulusBundle/src/Form/Extension/FormTypeExtension.php b/src/StimulusBundle/src/Form/Extension/FormTypeExtension.php index f528a05b9a9..b0c281ace3f 100644 --- a/src/StimulusBundle/src/Form/Extension/FormTypeExtension.php +++ b/src/StimulusBundle/src/Form/Extension/FormTypeExtension.php @@ -1,7 +1,5 @@ stimulusAttributes = new StimulusAttributes(new Environment(new ArrayLoader())); - $this->stimulusAttributes = new StimulusAttributes(new Environment(new ArrayLoader())); + if (isset($options['stimulus_controller'])) { + $this->handleController($options['stimulus_controller']); + } - if (true === \array_key_exists('stimulus_controller', $options)) { - $this->handleController($options['stimulus_controller']); - } + if (isset($options['stimulus_target'])) { + $this->handleTarget($options['stimulus_target']); + } - if (true === \array_key_exists('stimulus_target', $options)) { - $this->handleTarget($options['stimulus_target']); - } + if (isset($options['stimulus_action'])) { + $this->handleAction($options['stimulus_action']); + } - if (true === \array_key_exists('stimulus_action', $options)) { - $this->handleAction($options['stimulus_action']); + $attributes = array_merge($view->vars['attr'], $this->stimulusAttributes->toArray()); + $view->vars['attr'] = $attributes; } - $attributes = array_merge($view->vars['attr'], $this->stimulusAttributes->toArray()); + foreach (['row_attr', 'choice_attr'] as $index) { + if ( + isset($options[$index]) + && ( + isset($options[$index]['stimulus_controller']) + || isset($options[$index]['stimulus_target']) + || isset($options[$index]['stimulus_action']) + ) + ) { + $this->stimulusAttributes = new StimulusAttributes(new Environment(new ArrayLoader())); + + if (isset($options[$index]['stimulus_controller'])) { + $this->handleController($options[$index]['stimulus_controller']); + unset($options[$index]['stimulus_controller']); + } + + if (isset($options[$index]['stimulus_target'])) { + $this->handleTarget($options[$index]['stimulus_target']); + unset($options[$index]['stimulus_target']); + } + + if (isset($options[$index]['stimulus_action'])) { + $this->handleAction($options[$index]['stimulus_action']); + unset($options[$index]['stimulus_action']); + } - $view->vars['attr'] = $attributes; + $attributes = array_merge($options[$index], $this->stimulusAttributes->toArray()); + $view->vars[$index] = $attributes; + } + } } private function handleController(string|array $controllers): void { if (\is_string($controllers)) { - $controllers = [$controllcers]; + $controllers = [$controllers]; } foreach ($controllers as $controllerName => $controller) { - if (\is_string($controller)) { // 'stimulus_controller' => ['controllerName1', 'controllerName2'] + if (is_string($controller)) { // 'stimulus_controller' => ['controllerName1', 'controllerName2'] $this->stimulusAttributes->addController($controller); - } elseif (\is_array($controller)) { // 'stimulus_controller' => ['controllerName' => ['values' => ['key' => 'value'], 'classes' => ['key' => 'value'], 'targets' => ['otherControllerName' => '.targetName']]] + } elseif (is_array($controller)) { // 'stimulus_controller' => ['controllerName' => ['values' => ['key' => 'value'], 'classes' => ['key' => 'value'], 'targets' => ['otherControllerName' => '.targetName']]] $this->stimulusAttributes->addController((string) $controllerName, $controller['values'] ?? [], $controller['classes'] ?? [], $controller['outlets'] ?? []); } } @@ -78,7 +111,7 @@ private function handleController(string|array $controllers): void private function handleTarget(array $targets): void { foreach ($targets as $controllerName => $target) { - $this->stimulusAttributes->addTarget($controllerName, \is_array($target) ? implode(' ', $target) : $target); + $this->stimulusAttributes->addTarget($controllerName, is_array($target) ? implode(' ', $target) : $target); } } @@ -86,7 +119,7 @@ private function handleAction(string|array $actions): void { // 'stimulus_action' => 'controllerName#actionName' // 'stimulus_action' => 'eventName->controllerName#actionName' - if (\is_string($actions) && str_contains($actions, '#')) { + if (is_string($actions) && str_contains($actions, '#')) { $eventName = null; if (str_contains($actions, '->')) { @@ -103,13 +136,13 @@ private function handleAction(string|array $actions): void } foreach ($actions as $controllerName => $action) { - if (\is_string($action)) { // 'stimulus_action' => ['controllerName' => 'actionName'] + if (is_string($action)) { // 'stimulus_action' => ['controllerName' => 'actionName'] $this->stimulusAttributes->addAction($controllerName, $action); - } elseif (\is_array($action)) { + } elseif (is_array($action)) { foreach ($action as $eventName => $actionName) { - if (\is_string($actionName)) { // 'stimulus_action' => ['controllerName' => ['eventName' => 'actionName']] + if (is_string($actionName)) { // 'stimulus_action' => ['controllerName' => ['eventName' => 'actionName']] $this->stimulusAttributes->addAction($controllerName, $actionName, $eventName); - } elseif (\is_array($actionName)) { // 'stimulus_action' => ['controllerName' => ['eventName' => ['actionName' => ['key' => 'value']]]] + } elseif (is_array($actionName)) { // 'stimulus_action' => ['controllerName' => ['eventName' => ['actionName' => ['key' => 'value']]]] foreach ($actionName as $index => $params) { $this->stimulusAttributes->addAction($controllerName, $index, $eventName, $params); } @@ -124,13 +157,13 @@ public function configureOptions(OptionsResolver $resolver): void parent::configureOptions($resolver); $resolver->setDefaults([ - 'stimulus_action' => null, + 'stimulus_action' => null, 'stimulus_controller' => null, - 'stimulus_target' => null, + 'stimulus_target' => null, ]); - $resolver->setAllowedTypes('stimulus_action', ['string', 'array', 'null']); - $resolver->setAllowedTypes('stimulus_controller', ['string', 'array', 'null']); - $resolver->setAllowedTypes('stimulus_target', ['string', 'array', 'null']); + $resolver->setAllowedTypes('stimulus_action', ['string', 'array', 'callable', 'null']); + $resolver->setAllowedTypes('stimulus_controller', ['string', 'array', 'callable', 'null']); + $resolver->setAllowedTypes('stimulus_target', ['string', 'array', 'callable', 'null']); } }