Skip to content

Commit

Permalink
Merge 4.x into 5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
SonataCI authored Jul 29, 2023
2 parents 452cadc + 77cb5ee commit c519590
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [4.10.2](https://github.com/sonata-project/SonataMediaBundle/compare/4.10.1...4.10.2) - 2023-07-29
### Changed
- [[#2402](https://github.com/sonata-project/SonataMediaBundle/pull/2402)] Resole "Admin has no subject" error from the getMediaBuilder ([@Elkawaie](https://github.com/Elkawaie))

## [4.10.1](https://github.com/sonata-project/SonataMediaBundle/compare/4.10.0...4.10.1) - 2023-07-22
### Fixed
- [[#2405](https://github.com/sonata-project/SonataMediaBundle/pull/2405)] * `round()` can return 0 which is invalid for image resizing. ([@silasjoisten](https://github.com/silasjoisten))
Expand Down
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 c519590

Please sign in to comment.