Skip to content

Commit

Permalink
Update getMediaBuilder method
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximilien committed Jun 23, 2023
1 parent fe82cd8 commit 9650bea
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/Block/MediaBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function configureEditForm(FormMapper $form, BlockInterface $block): void
'label' => 'form.label_class',
'required' => false,
]],
[$this->getMediaBuilder(), null, []],
[$this->getMediaBuilder($form), null, []],
['format', ChoiceType::class, [
'required' => \count($formatChoices) > 0,
'choices' => $formatChoices,
Expand Down Expand Up @@ -195,7 +195,10 @@ private function getFormatChoices(?MediaInterface $media = null): array
return $formatChoices;
}

private function getMediaBuilder(): FormBuilderInterface
/**
* @param AdminFormMapper<object> $form
*/
private function getMediaBuilder(FormMapper $form): FormBuilderInterface
{
if (null === $this->mediaAdmin) {
throw new \LogicException('The SonataAdminBundle is required to render the edit form.');
Expand All @@ -208,14 +211,17 @@ private function getMediaBuilder(): FormBuilderInterface

$fieldDescription->setAssociationAdmin($this->mediaAdmin);

$formBuilder = $this->mediaAdmin->getFormBuilder()->create('mediaId', ModelListType::class, [
$formBuilder = $form->getFormBuilder()->create('mediaId', ModelListType::class, [
'sonata_field_description' => $fieldDescription,
'class' => $this->mediaAdmin->getClass(),
'model_manager' => $this->mediaAdmin->getModelManager(),
'label' => 'form.label_media',
]);

// Add ModelTransformer ?
$formBuilder->addModelTransformer(new CallbackTransformer(
static fn (mixed $value): ?PageBlockInterface => $value,
static fn (?PageBlockInterface $value) => $value instanceof PageBlockInterface ? $value->getId() : $value
));

return $formBuilder;
}
Expand Down

0 comments on commit 9650bea

Please sign in to comment.