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 committed Nov 19, 2021
2 parents bc4a12b + bc4c763 commit 652d1f5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
42 changes: 25 additions & 17 deletions src/Block/GalleryBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,19 +45,19 @@ final class GalleryBlockService extends AbstractBlockService implements Editable
private Pool $pool;

/**
* @var AdminInterface<GalleryInterface>
* @var AdminInterface<GalleryInterface>|null
*/
private AdminInterface $galleryAdmin;
private ?AdminInterface $galleryAdmin;

private GalleryManagerInterface $galleryManager;

/**
* @param AdminInterface<GalleryInterface> $galleryAdmin
* @param AdminInterface<GalleryInterface>|null $galleryAdmin
*/
public function __construct(
Environment $twig,
Pool $pool,
AdminInterface $galleryAdmin,
?AdminInterface $galleryAdmin,
GalleryManagerInterface $galleryManager
) {
parent::__construct($twig);
Expand Down Expand Up @@ -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, [
Expand Down Expand Up @@ -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',
]],
Expand Down Expand Up @@ -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<string, MediaInterface|string|null>
*
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/block.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
])

Expand Down
6 changes: 5 additions & 1 deletion tests/Block/GalleryBlockServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Sonata\MediaBundle\Tests\Block;

use Doctrine\Common\Collections\ArrayCollection;
use PHPUnit\Framework\MockObject\Stub;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\BlockBundle\Block\BlockContextInterface;
use Sonata\BlockBundle\Model\Block;
Expand All @@ -31,10 +32,13 @@ protected function setUp(): void
{
parent::setUp();

/** @var AdminInterface<GalleryInterface>&Stub $galleryAdmin */
$galleryAdmin = $this->createStub(AdminInterface::class);

$this->blockService = new GalleryBlockService(
$this->twig,
new Pool('default'),
$this->createStub(AdminInterface::class),
$galleryAdmin,
$this->createStub(GalleryManagerInterface::class)
);
}
Expand Down

0 comments on commit 652d1f5

Please sign in to comment.