Skip to content

Commit f8b426f

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

File tree

1 file changed

+65
-32
lines changed

1 file changed

+65
-32
lines changed

Diff for: src/StimulusBundle/src/Form/Extension/FormTypeExtension.php

+65-32
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
*
@@ -22,6 +20,13 @@
2220
use Twig\Environment;
2321
use Twig\Loader\ArrayLoader;
2422

23+
use function array_merge;
24+
use function explode;
25+
use function implode;
26+
use function is_array;
27+
use function is_string;
28+
use function str_contains;
29+
2530
class FormTypeExtension extends AbstractTypeExtension
2631
{
2732
private StimulusAttributes $stimulusAttributes;
@@ -34,42 +39,70 @@ public static function getExtendedTypes(): iterable
3439
public function buildView(FormView $view, FormInterface $form, array $options): void
3540
{
3641
if (
37-
null === $options['stimulus_controller']
38-
&& null === $options['stimulus_target']
39-
&& null === $options['stimulus_action']
42+
isset($options['stimulus_controller'])
43+
|| !isset($options['stimulus_target'])
44+
|| !isset($options['stimulus_action'])
4045
) {
41-
return;
42-
}
46+
$this->stimulusAttributes = new StimulusAttributes(new Environment(new ArrayLoader()));
4347

44-
$this->stimulusAttributes = new StimulusAttributes(new Environment(new ArrayLoader()));
48+
if (isset($options['stimulus_controller'])) {
49+
$this->handleController($options['stimulus_controller']);
50+
}
4551

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

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

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

58-
$attributes = array_merge($view->vars['attr'], $this->stimulusAttributes->toArray());
64+
foreach (['row_attr', 'choice_attr'] as $index) {
65+
if (
66+
isset($options[$index])
67+
&& (
68+
isset($options[$index]['stimulus_controller'])
69+
|| isset($options[$index]['stimulus_target'])
70+
|| isset($options[$index]['stimulus_action'])
71+
)
72+
) {
73+
$this->stimulusAttributes = new StimulusAttributes(new Environment(new ArrayLoader()));
74+
75+
if (isset($options[$index]['stimulus_controller'])) {
76+
$this->handleController($options[$index]['stimulus_controller']);
77+
unset($options[$index]['stimulus_controller']);
78+
}
79+
80+
if (isset($options[$index]['stimulus_target'])) {
81+
$this->handleTarget($options[$index]['stimulus_target']);
82+
unset($options[$index]['stimulus_target']);
83+
}
84+
85+
if (isset($options[$index]['stimulus_action'])) {
86+
$this->handleAction($options[$index]['stimulus_action']);
87+
unset($options[$index]['stimulus_action']);
88+
}
5989

60-
$view->vars['attr'] = $attributes;
90+
$attributes = array_merge($options[$index], $this->stimulusAttributes->toArray());
91+
$view->vars[$index] = $attributes;
92+
}
93+
}
6194
}
6295

6396
private function handleController(string|array $controllers): void
6497
{
6598
if (\is_string($controllers)) {
66-
$controllers = [$controllcers];
99+
$controllers = [$controllers];
67100
}
68101

69102
foreach ($controllers as $controllerName => $controller) {
70-
if (\is_string($controller)) { // 'stimulus_controller' => ['controllerName1', 'controllerName2']
103+
if (is_string($controller)) { // 'stimulus_controller' => ['controllerName1', 'controllerName2']
71104
$this->stimulusAttributes->addController($controller);
72-
} elseif (\is_array($controller)) { // 'stimulus_controller' => ['controllerName' => ['values' => ['key' => 'value'], 'classes' => ['key' => 'value'], 'targets' => ['otherControllerName' => '.targetName']]]
105+
} elseif (is_array($controller)) { // 'stimulus_controller' => ['controllerName' => ['values' => ['key' => 'value'], 'classes' => ['key' => 'value'], 'targets' => ['otherControllerName' => '.targetName']]]
73106
$this->stimulusAttributes->addController((string) $controllerName, $controller['values'] ?? [], $controller['classes'] ?? [], $controller['outlets'] ?? []);
74107
}
75108
}
@@ -78,15 +111,15 @@ private function handleController(string|array $controllers): void
78111
private function handleTarget(array $targets): void
79112
{
80113
foreach ($targets as $controllerName => $target) {
81-
$this->stimulusAttributes->addTarget($controllerName, \is_array($target) ? implode(' ', $target) : $target);
114+
$this->stimulusAttributes->addTarget($controllerName, is_array($target) ? implode(' ', $target) : $target);
82115
}
83116
}
84117

85118
private function handleAction(string|array $actions): void
86119
{
87120
// 'stimulus_action' => 'controllerName#actionName'
88121
// 'stimulus_action' => 'eventName->controllerName#actionName'
89-
if (\is_string($actions) && str_contains($actions, '#')) {
122+
if (is_string($actions) && str_contains($actions, '#')) {
90123
$eventName = null;
91124

92125
if (str_contains($actions, '->')) {
@@ -103,13 +136,13 @@ private function handleAction(string|array $actions): void
103136
}
104137

105138
foreach ($actions as $controllerName => $action) {
106-
if (\is_string($action)) { // 'stimulus_action' => ['controllerName' => 'actionName']
139+
if (is_string($action)) { // 'stimulus_action' => ['controllerName' => 'actionName']
107140
$this->stimulusAttributes->addAction($controllerName, $action);
108-
} elseif (\is_array($action)) {
141+
} elseif (is_array($action)) {
109142
foreach ($action as $eventName => $actionName) {
110-
if (\is_string($actionName)) { // 'stimulus_action' => ['controllerName' => ['eventName' => 'actionName']]
143+
if (is_string($actionName)) { // 'stimulus_action' => ['controllerName' => ['eventName' => 'actionName']]
111144
$this->stimulusAttributes->addAction($controllerName, $actionName, $eventName);
112-
} elseif (\is_array($actionName)) { // 'stimulus_action' => ['controllerName' => ['eventName' => ['actionName' => ['key' => 'value']]]]
145+
} elseif (is_array($actionName)) { // 'stimulus_action' => ['controllerName' => ['eventName' => ['actionName' => ['key' => 'value']]]]
113146
foreach ($actionName as $index => $params) {
114147
$this->stimulusAttributes->addAction($controllerName, $index, $eventName, $params);
115148
}
@@ -124,13 +157,13 @@ public function configureOptions(OptionsResolver $resolver): void
124157
parent::configureOptions($resolver);
125158

126159
$resolver->setDefaults([
127-
'stimulus_action' => null,
160+
'stimulus_action' => null,
128161
'stimulus_controller' => null,
129-
'stimulus_target' => null,
162+
'stimulus_target' => null,
130163
]);
131164

132-
$resolver->setAllowedTypes('stimulus_action', ['string', 'array', 'null']);
133-
$resolver->setAllowedTypes('stimulus_controller', ['string', 'array', 'null']);
134-
$resolver->setAllowedTypes('stimulus_target', ['string', 'array', 'null']);
165+
$resolver->setAllowedTypes('stimulus_action', ['string', 'array', 'callable', 'null']);
166+
$resolver->setAllowedTypes('stimulus_controller', ['string', 'array', 'callable', 'null']);
167+
$resolver->setAllowedTypes('stimulus_target', ['string', 'array', 'callable', 'null']);
135168
}
136169
}

0 commit comments

Comments
 (0)