Skip to content

Commit

Permalink
Added support for row_attr and choice_attr
Browse files Browse the repository at this point in the history
  • Loading branch information
19Gerhard85 committed Dec 19, 2024
1 parent be4394f commit 92e2a5e
Showing 1 changed file with 48 additions and 22 deletions.
70 changes: 48 additions & 22 deletions src/StimulusBundle/src/Form/Extension/FormTypeExtension.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Symfony package.
*
Expand Down Expand Up @@ -34,36 +32,64 @@ public static function getExtendedTypes(): iterable
public function buildView(FormView $view, FormInterface $form, array $options): void
{
if (
null === $options['stimulus_controller']
&& null === $options['stimulus_target']
&& null === $options['stimulus_action']
isset($options['stimulus_controller'])
|| !isset($options['stimulus_target'])
|| !isset($options['stimulus_action'])
) {
return;
}
$this->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']);
}

$view->vars['attr'] = $attributes;
if (isset($options[$index]['stimulus_action'])) {
$this->handleAction($options[$index]['stimulus_action']);
unset($options[$index]['stimulus_action']);
}

$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) {
Expand Down Expand Up @@ -129,8 +155,8 @@ public function configureOptions(OptionsResolver $resolver): void
'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']);
}
}

0 comments on commit 92e2a5e

Please sign in to comment.