Skip to content

Commit

Permalink
Update getMediaBuilder Admin (#2402)
Browse files Browse the repository at this point in the history
Co-authored-by: Vincent Langlet <VincentLanglet@users.noreply.github.com>
Co-authored-by: Maximilien <mdelangle@prestaconcept.net>
  • Loading branch information
3 people authored Jul 28, 2023
1 parent 7c49969 commit 9051e2c
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/Block/MediaBlockService.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\TextType;
use Symfony\Component\Form\FormBuilderInterface;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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,
Expand Down Expand Up @@ -193,7 +199,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 @@ -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;
}
}

0 comments on commit 9051e2c

Please sign in to comment.