Skip to content

Commit

Permalink
Fix getMediaBuilder and getGalleryBuilder (#2442)
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet authored Feb 18, 2024
1 parent 1ac734f commit ca4e236
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
23 changes: 20 additions & 3 deletions src/Block/FeatureMediaBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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, [
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -204,7 +210,10 @@ private function getFormatChoices(?MediaInterface $media = null): array
return $formatChoices;
}

private function getMediaBuilder(): FormBuilderInterface
/**
* @param AdminFormMapper<object> $form
*/
private function getMediaBuilder(AdminFormMapper $form): FormBuilderInterface
{
if (null === $this->mediaAdmin) {
throw new \LogicException('The SonataAdminBundle is required to render the edit form.');
Expand All @@ -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;
}
}
23 changes: 20 additions & 3 deletions src/Block/GalleryBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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',
]],
Expand Down Expand Up @@ -183,7 +189,10 @@ public function validate(ErrorElement $errorElement, BlockInterface $block): voi
{
}

private function getGalleryBuilder(): FormBuilderInterface
/**
* @param AdminFormMapper<object> $form
*/
private function getGalleryBuilder(AdminFormMapper $form): FormBuilderInterface
{
if (null === $this->galleryAdmin) {
throw new \LogicException('The SonataAdminBundle is required to render the edit form.');
Expand All @@ -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;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Block/MediaBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit ca4e236

Please sign in to comment.