From ca4e23623f0abbe254e81fe7f6acb92d7c71e7a5 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sun, 18 Feb 2024 16:16:48 +0100 Subject: [PATCH] Fix getMediaBuilder and getGalleryBuilder (#2442) --- src/Block/FeatureMediaBlockService.php | 23 ++++++++++++++++++++--- src/Block/GalleryBlockService.php | 23 ++++++++++++++++++++--- src/Block/MediaBlockService.php | 7 ++++--- 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/Block/FeatureMediaBlockService.php b/src/Block/FeatureMediaBlockService.php index eccc08ffb..00fd405ff 100644 --- a/src/Block/FeatureMediaBlockService.php +++ b/src/Block/FeatureMediaBlockService.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\TextareaType; use Symfony\Component\Form\Extension\Core\Type\TextType; @@ -71,6 +73,10 @@ public function configureSettings(OptionsResolver $resolver): void public function configureEditForm(FormMapper $form, BlockInterface $block): void { + if (!$form instanceof AdminFormMapper) { + throw new \InvalidArgumentException('Feature media block requires to be used in the Admin context'); + } + $formatChoices = $this->getFormatChoices($block->getSetting('mediaId')); $form->add('settings', ImmutableArrayType::class, [ @@ -103,7 +109,7 @@ public function configureEditForm(FormMapper $form, BlockInterface $block): void ], 'label' => 'form.label_orientation', ]], - [$this->getMediaBuilder(), null, []], + [$this->getMediaBuilder($form), null, []], ['format', ChoiceType::class, [ 'required' => \count($formatChoices) > 0, 'choices' => $formatChoices, @@ -204,7 +210,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.'); @@ -214,12 +223,20 @@ private function getMediaBuilder(): FormBuilderInterface 'translation_domain' => 'SonataMediaBundle', 'edit' => 'list', ]); + $fieldDescription->setAssociationAdmin($this->mediaAdmin); - return $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', ]); + + $formBuilder->addModelTransformer(new CallbackTransformer( + static fn (?MediaInterface $value): ?MediaInterface => $value, + static fn (?MediaInterface $value) => $value instanceof MediaInterface ? $value->getId() : $value + )); + + return $formBuilder; } } diff --git a/src/Block/GalleryBlockService.php b/src/Block/GalleryBlockService.php index 45533213f..ca003ccbb 100644 --- a/src/Block/GalleryBlockService.php +++ b/src/Block/GalleryBlockService.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; @@ -29,6 +30,7 @@ use Sonata\MediaBundle\Model\GalleryManagerInterface; use Sonata\MediaBundle\Model\MediaInterface; use Sonata\MediaBundle\Provider\Pool; +use Symfony\Component\Form\CallbackTransformer; use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\NumberType; @@ -74,6 +76,10 @@ public function configureSettings(OptionsResolver $resolver): void public function configureEditForm(FormMapper $form, BlockInterface $block): void { + if (!$form instanceof AdminFormMapper) { + throw new \InvalidArgumentException('Gallery block requires to be used in the Admin context'); + } + $contextChoices = []; foreach ($this->pool->getContexts() as $name => $context) { @@ -124,7 +130,7 @@ public function configureEditForm(FormMapper $form, BlockInterface $block): void 'choices' => $formatChoices, 'label' => 'form.label_format', ]], - [$this->getGalleryBuilder(), null, []], + [$this->getGalleryBuilder($form), null, []], ['pauseTime', NumberType::class, [ 'label' => 'form.label_pause_time', ]], @@ -183,7 +189,10 @@ public function validate(ErrorElement $errorElement, BlockInterface $block): voi { } - private function getGalleryBuilder(): FormBuilderInterface + /** + * @param AdminFormMapper $form + */ + private function getGalleryBuilder(AdminFormMapper $form): FormBuilderInterface { if (null === $this->galleryAdmin) { throw new \LogicException('The SonataAdminBundle is required to render the edit form.'); @@ -193,13 +202,21 @@ private function getGalleryBuilder(): FormBuilderInterface 'translation_domain' => 'SonataMediaBundle', 'edit' => 'list', ]); + $fieldDescription->setAssociationAdmin($this->galleryAdmin); - return $this->galleryAdmin->getFormBuilder()->create('galleryId', ModelListType::class, [ + $formBuilder = $form->getFormBuilder()->create('galleryId', ModelListType::class, [ 'sonata_field_description' => $fieldDescription, 'class' => $this->galleryAdmin->getClass(), 'model_manager' => $this->galleryAdmin->getModelManager(), 'label' => 'form.label_gallery', ]); + + $formBuilder->addModelTransformer(new CallbackTransformer( + static fn (?MediaInterface $value): ?MediaInterface => $value, + static fn (?MediaInterface $value) => $value instanceof MediaInterface ? $value->getId() : $value + )); + + return $formBuilder; } /** diff --git a/src/Block/MediaBlockService.php b/src/Block/MediaBlockService.php index fc7ce012a..17e7e47a1 100644 --- a/src/Block/MediaBlockService.php +++ b/src/Block/MediaBlockService.php @@ -25,6 +25,7 @@ use Sonata\BlockBundle\Model\BlockInterface; use Sonata\Form\Type\ImmutableArrayType; use Sonata\Form\Validator\ErrorElement; +use Sonata\MediaBundle\Model\GalleryInterface; use Sonata\MediaBundle\Model\MediaInterface; use Sonata\MediaBundle\Model\MediaManagerInterface; use Sonata\MediaBundle\Provider\Pool; @@ -71,7 +72,7 @@ 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'); + throw new \InvalidArgumentException('Media block requires to be used in the Admin context'); } if (!$block->getSetting('mediaId') instanceof MediaInterface) { @@ -223,8 +224,8 @@ private function getMediaBuilder(AdminFormMapper $form): FormBuilderInterface ]); $formBuilder->addModelTransformer(new CallbackTransformer( - static fn (?MediaInterface $value): ?MediaInterface => $value, - static fn (?MediaInterface $value) => $value instanceof MediaInterface ? $value->getId() : $value + static fn (?GalleryInterface $value): ?GalleryInterface => $value, + static fn (?GalleryInterface $value) => $value instanceof GalleryInterface ? $value->getId() : $value )); return $formBuilder;