From 9051e2c917db31158e01c755b3f4b1616fa77731 Mon Sep 17 00:00:00 2001 From: Maximilien Delangle <40693136+Elkawaie@users.noreply.github.com> Date: Fri, 28 Jul 2023 09:35:48 +0200 Subject: [PATCH] Update getMediaBuilder Admin (#2402) Co-authored-by: Vincent Langlet Co-authored-by: Maximilien --- src/Block/MediaBlockService.php | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/Block/MediaBlockService.php b/src/Block/MediaBlockService.php index 51159fc42..fc7ce012a 100644 --- a/src/Block/MediaBlockService.php +++ b/src/Block/MediaBlockService.php @@ -14,6 +14,7 @@ namespace Sonata\MediaBundle\Block; use Sonata\AdminBundle\Admin\AdminInterface; +use Sonata\AdminBundle\Form\FormMapper as AdminFormMapper; use Sonata\AdminBundle\Form\Type\ModelListType; use Sonata\BlockBundle\Block\BlockContextInterface; use Sonata\BlockBundle\Block\Service\AbstractBlockService; @@ -27,6 +28,7 @@ use Sonata\MediaBundle\Model\MediaInterface; use Sonata\MediaBundle\Model\MediaManagerInterface; use Sonata\MediaBundle\Provider\Pool; +use Symfony\Component\Form\CallbackTransformer; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; @@ -68,6 +70,10 @@ public function configureSettings(OptionsResolver $resolver): void public function configureEditForm(FormMapper $form, BlockInterface $block): void { + if (!$form instanceof AdminFormMapper) { + throw new \InvalidArgumentException('Media Block requires to be used in the Admin context'); + } + if (!$block->getSetting('mediaId') instanceof MediaInterface) { $this->load($block); } @@ -92,7 +98,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, @@ -193,7 +199,10 @@ private function getFormatChoices(?MediaInterface $media = null): array return $formatChoices; } - private function getMediaBuilder(): FormBuilderInterface + /** + * @param AdminFormMapper $form + */ + private function getMediaBuilder(AdminFormMapper $form): FormBuilderInterface { if (null === $this->mediaAdmin) { throw new \LogicException('The SonataAdminBundle is required to render the edit form.'); @@ -204,11 +213,20 @@ private function getMediaBuilder(): FormBuilderInterface 'edit' => 'list', ]); - return $this->mediaAdmin->getFormBuilder()->create('mediaId', ModelListType::class, [ + $fieldDescription->setAssociationAdmin($this->mediaAdmin); + + $formBuilder = $form->getFormBuilder()->create('mediaId', ModelListType::class, [ 'sonata_field_description' => $fieldDescription, 'class' => $this->mediaAdmin->getClass(), 'model_manager' => $this->mediaAdmin->getModelManager(), 'label' => 'form.label_media', ]); + + $formBuilder->addModelTransformer(new CallbackTransformer( + static fn (?MediaInterface $value): ?MediaInterface => $value, + static fn (?MediaInterface $value) => $value instanceof MediaInterface ? $value->getId() : $value + )); + + return $formBuilder; } }