Skip to content

Instantly share code, notes, and snippets.

@covex-nn
Created March 21, 2018 06:27
Show Gist options
  • Save covex-nn/1cb75d95b6805768a4a20eb673726782 to your computer and use it in GitHub Desktop.
Save covex-nn/1cb75d95b6805768a4a20eb673726782 to your computer and use it in GitHub Desktop.
MediaType as a child form type
<?php
declare(strict_types=1);
namespace App\Block;
use App\Entity\SonataMediaMedia;
use App\Traits\EntityManagerTrait;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\BlockBundle\Block\Service\AbstractAdminBlockService;
use Sonata\BlockBundle\Model\BlockInterface;
abstract class AbstractMediaUploaderBlock extends AbstractAdminBlockService
{
use EntityManagerTrait;
protected $medias = [];
public function buildEditForm(FormMapper $formMapper, BlockInterface $block): void
{
$this->loadMedias($block);
}
public function prePersist(BlockInterface $block): void
{
parent::prePersist($block);
$this->saveMedias($block);
}
public function preUpdate(BlockInterface $block): void
{
parent::preUpdate($block);
$this->saveMedias($block);
}
protected function loadMedias(BlockInterface $block): void
{
$repository = $this->em->getRepository(SonataMediaMedia::class);
foreach ($this->medias as $name) {
$file = $block->getSetting($name);
if (null !== $file) {
if (is_numeric($file)) {
$file = $repository->find($file);
}
if (!$file instanceof SonataMediaMedia) {
$file = null;
}
$block->setSetting($name, $file);
}
}
}
protected function saveMedias(BlockInterface $block): void
{
foreach ($this->medias as $name) {
$file = $block->getSetting($name);
if ($file instanceof SonataMediaMedia) {
if ($file->getSize()) {
$this->em->persist($file);
$this->em->flush();
$block->setSetting($name, $file->getId());
}
}
}
}
}
<?php
declare(strict_types=1);
namespace App\Block;
use App\Form\Type\LayoutFormType;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\BlockBundle\Block\BlockContextInterface;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\CoreBundle\Form\Type\ImmutableArrayType;
use Sonata\CoreBundle\Model\Metadata;
use Sonata\FormatterBundle\Form\Type\FormatterType;
use Sonata\MediaBundle\Admin\BaseMediaAdmin;
use Sonata\MediaBundle\Form\Type\MediaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\OptionsResolver\OptionsResolver;
class TextBannerBlock extends AbstractMediaUploaderBlock
{
protected $medias = ['background'];
/**
* @var BaseMediaAdmin
*/
private $mediaAdmin;
public function execute(BlockContextInterface $blockContext, Response $response = null)
{
return $this->renderResponse($blockContext->getTemplate(), [
'block' => $blockContext->getBlock(),
'settings' => $blockContext->getSettings(),
], $response);
}
public function buildEditForm(FormMapper $formMapper, BlockInterface $block): void
{
parent::buildEditForm($formMapper, $block);
$fieldDescription = $this->mediaAdmin->getModelManager()->getNewFieldDescriptionInstance(
$this->mediaAdmin->getClass(), 'media', ['translation_domain' => 'SonataMediaBundle']
);
$fieldDescription->setAdmin($formMapper->getAdmin());
$formMapper->add('settings', ImmutableArrayType::class, [
'keys' => [
['content', FormatterType::class, function (FormBuilderInterface $formBuilder) {
return [
'event_dispatcher' => $formBuilder->getEventDispatcher(),
'format_field' => ['format', '[format]'],
'source_field' => ['rawContent', '[rawContent]'],
'target_field' => '[content]',
'label' => 'form.label_content',
];
}],
['background', MediaType::class, [
'sonata_field_description' => $fieldDescription,
'provider' => 'sonata.media.provider.image',
'required' => false,
'context' => 'banners',
]],
['layout', LayoutFormType::class, [
'required' => false,
]],
],
'translation_domain' => 'SonataFormatterBundle',
]);
}
public function configureSettings(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'format' => 'richhtml',
'rawContent' => '<b>Insert your custom content here</b>',
'content' => '<b>Insert your custom content here</b>',
'background' => null,
'layout' => [],
'template' => 'Block/text_banner.html.twig',
]);
}
public function getBlockMetadata($code = null): Metadata
{
return new Metadata(
$this->getName(), $code ?? $this->getName(), false, 'AppBlocks', [
'class' => 'fa fa-file-text-o',
]
);
}
/**
* @required
*/
public function setMediaAdmin(BaseMediaAdmin $mediaAdmin): void
{
$this->mediaAdmin = $mediaAdmin;
}
}
<?php
declare(strict_types=1);
namespace App\Traits;
use Doctrine\ORM\EntityManagerInterface;
trait EntityManagerTrait
{
/**
* @var EntityManagerInterface
*/
protected $em;
/**
* @required
*/
public function setEntityManager(EntityManagerInterface $em): void
{
$this->em = $em;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment