diff --git a/src/Block/GalleryBlockService.php b/src/Block/GalleryBlockService.php index c114be332a..f3ba33b5a4 100644 --- a/src/Block/GalleryBlockService.php +++ b/src/Block/GalleryBlockService.php @@ -32,6 +32,7 @@ use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\NumberType; use Symfony\Component\Form\Extension\Core\Type\TextType; +use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\OptionsResolver\OptionsResolver; use Twig\Environment; @@ -44,20 +45,20 @@ final class GalleryBlockService extends AbstractBlockService implements Editable private Pool $pool; /** - * @var AdminInterface + * @var AdminInterface|null */ - private AdminInterface $galleryAdmin; + private ?AdminInterface $galleryAdmin; private GalleryManagerInterface $galleryManager; /** - * @param AdminInterface $galleryAdmin + * @param AdminInterface|null $galleryAdmin */ public function __construct( Environment $twig, Pool $pool, AdminInterface $galleryAdmin, - GalleryManagerInterface $galleryManager + ?GalleryManagerInterface $galleryManager ) { parent::__construct($twig); @@ -107,18 +108,6 @@ public function configureEditForm(FormMapper $form, BlockInterface $block): void } } - $fieldDescription = $this->galleryAdmin->createFieldDescription('media', [ - 'translation_domain' => 'SonataMediaBundle', - 'edit' => 'list', - ]); - - $builder = $form->create('galleryId', ModelListType::class, [ - 'sonata_field_description' => $fieldDescription, - 'class' => $this->galleryAdmin->getClass(), - 'model_manager' => $this->galleryAdmin->getModelManager(), - 'label' => 'form.label_gallery', - ]); - $form->add('settings', ImmutableArrayType::class, [ 'keys' => [ ['title', TextType::class, [ @@ -147,7 +136,7 @@ public function configureEditForm(FormMapper $form, BlockInterface $block): void 'choices' => $formatChoices, 'label' => 'form.label_format', ]], - [$builder, null, []], + [$this->getGalleryBuilder($form), null, []], ['pauseTime', NumberType::class, [ 'label' => 'form.label_pause_time', ]], @@ -207,6 +196,25 @@ public function validate(ErrorElement $errorElement, BlockInterface $block): voi { } + private function getGalleryBuilder(FormMapper $form): FormBuilderInterface + { + if (null === $this->galleryAdmin) { + throw new \LogicException('The SonataAdminBundle is required to render the edit form.'); + } + + $fieldDescription = $this->galleryAdmin->createFieldDescription('gallery', [ + 'translation_domain' => 'SonataMediaBundle', + 'edit' => 'list', + ]); + + return $form->create('galleryId', ModelListType::class, [ + 'sonata_field_description' => $fieldDescription, + 'class' => $this->galleryAdmin->getClass(), + 'model_manager' => $this->galleryAdmin->getModelManager(), + 'label' => 'form.label_gallery', + ]); + } + /** * @return array * diff --git a/src/Resources/config/block.php b/src/Resources/config/block.php index f42991525e..815e9858bd 100644 --- a/src/Resources/config/block.php +++ b/src/Resources/config/block.php @@ -45,7 +45,7 @@ ->args([ new ReferenceConfigurator('twig'), new ReferenceConfigurator('sonata.media.pool'), - new ReferenceConfigurator('sonata.media.admin.gallery'), + (new ReferenceConfigurator('sonata.media.admin.gallery'))->nullOnInvalid(), new ReferenceConfigurator('sonata.media.manager.gallery'), ])