Skip to content

Commit

Permalink
Make admin dependency optional for blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 committed Nov 17, 2021
1 parent dcd4ad0 commit a80a4a2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 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,20 +45,20 @@ 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,
GalleryManagerInterface $galleryManager
?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

0 comments on commit a80a4a2

Please sign in to comment.