Skip to content

Commit 03aaaea

Browse files
committed
fix(media-manager): fix issue in collection image with media manager
1 parent 0548eb7 commit 03aaaea

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/Twig/Components/UiElementForm.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
2020
use Symfony\UX\LiveComponent\Attribute\LiveAction;
2121
use Symfony\UX\LiveComponent\Attribute\LiveArg;
22+
use Symfony\UX\LiveComponent\Attribute\LiveListener;
2223
use Symfony\UX\LiveComponent\Attribute\LiveProp;
2324
use Symfony\UX\LiveComponent\ComponentToolsTrait;
2425
use Symfony\UX\LiveComponent\ComponentWithFormTrait;
@@ -76,4 +77,35 @@ protected function instantiateForm(): FormInterface
7677
$this->data,
7778
);
7879
}
80+
81+
#[LiveListener('media-manager:file-selected')]
82+
public function mediaManagerFileSelected(
83+
#[LiveArg]
84+
string $inputName,
85+
#[LiveArg]
86+
string $filePath,
87+
): void {
88+
// Support any depth: e.g. menu_item[linkSettings][thumbnail] or menu_item[foo][bar][baz]
89+
$inputNameParts = explode('[', str_replace(']', '', $inputName));
90+
// Remove the first part (form name, e.g. menu_item)
91+
array_shift($inputNameParts);
92+
$this->setFormValueByPath($inputNameParts, $filePath);
93+
}
94+
95+
/**
96+
* Set a value in formValues using a path array (e.g. ['linkSettings','thumbnail']).
97+
*
98+
* @param array<int, string> $path
99+
*/
100+
private function setFormValueByPath(array $path, mixed $value): void
101+
{
102+
$ref = &$this->formValues;
103+
foreach ($path as $segment) {
104+
if (!isset($ref[$segment]) || !\is_array($ref[$segment])) {
105+
$ref[$segment] = [];
106+
}
107+
$ref = &$ref[$segment];
108+
}
109+
$ref = $value;
110+
}
79111
}

0 commit comments

Comments
 (0)