Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/Twig/Components/UiElementForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\Attribute\LiveArg;
use Symfony\UX\LiveComponent\Attribute\LiveListener;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\ComponentToolsTrait;
use Symfony\UX\LiveComponent\ComponentWithFormTrait;
Expand Down Expand Up @@ -76,4 +77,35 @@ protected function instantiateForm(): FormInterface
$this->data,
);
}

#[LiveListener('media-manager:file-selected')]
public function mediaManagerFileSelected(
#[LiveArg]
string $inputName,
#[LiveArg]
string $filePath,
): void {
// Support any depth: e.g. menu_item[linkSettings][thumbnail] or menu_item[foo][bar][baz]
$inputNameParts = explode('[', str_replace(']', '', $inputName));
// Remove the first part (form name, e.g. menu_item)
array_shift($inputNameParts);
$this->setFormValueByPath($inputNameParts, $filePath);
}

/**
* Set a value in formValues using a path array (e.g. ['linkSettings','thumbnail']).
*
* @param array<int, string> $path
*/
private function setFormValueByPath(array $path, mixed $value): void
{
$ref = &$this->formValues;
foreach ($path as $segment) {
if (!isset($ref[$segment]) || !\is_array($ref[$segment])) {
$ref[$segment] = [];
}
$ref = &$ref[$segment];
}
$ref = $value;
}
}
Loading