add file block type for page

This commit is contained in:
Simon Vieille 2021-03-22 16:52:39 +01:00
parent b32b292c22
commit 36168cfb0f
11 changed files with 170 additions and 35 deletions

View File

@ -4,4 +4,4 @@ app:
App\Entity\Page\SimplePage:
name: 'Page simple'
templates:
- {name: "Template 1", file: "site/page/simple/page.html.twig"}
- {name: "Template 1", file: "page/simple/page.html.twig"}

View File

@ -8,6 +8,8 @@ use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=BlockRepository::class)
* @ORM\DiscriminatorColumn(name="class_key", type="string")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\HasLifecycleCallbacks
*/
class Block
@ -54,7 +56,7 @@ class Block
return $this;
}
public function getValue(): ?string
public function getValue()
{
return $this->value;
}

View File

@ -0,0 +1,36 @@
<?php
namespace App\Core\Entity\Site\Page;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
/**
* @ORM\Entity
*/
class FileBlock extends Block
{
public function getValue()
{
$value = parent::getValue();
if (is_string($value)) {
if (file_exists($value)) {
return new File($value);
}
return null;
}
return $value;
}
public function setValue($value): self
{
if ($this->getValue() instanceof File && null === $value) {
return $this;
}
return parent::setValue($value);
}
}

View File

@ -137,7 +137,7 @@ class Page implements EntityInterface
{
}
public function getBlock($name)
public function getBlock($name, string $className = null)
{
foreach ($this->getBlocks() as $block) {
if ($block->getName() === $name) {
@ -145,7 +145,12 @@ class Page implements EntityInterface
}
}
$block = new Block();
if ($className) {
$block = new $className();
} else {
$block = new Block();
}
$block->setName($name);
$block->setPage($this);

View File

@ -0,0 +1,14 @@
<?php
namespace App\Core\Entity\Site\Page;
use App\Core\Doctrine\Timestampable;
use App\Core\Repository\Site\Page\BlockRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class TextBlock extends Block
{
}

View File

@ -7,6 +7,8 @@ use App\Core\Entity\Site\Page\Block;
use App\Core\Event\EntityManager\EntityManagerEvent;
use App\Core\EventSuscriber\EntityManagerEventSubscriber;
use App\Core\Form\FileUploadHandler;
use App\Core\Entity\Site\Page\FileBlock;
use App\Core\Entity\Site\Page\Page;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
@ -25,7 +27,7 @@ class BlockEventSubscriber extends EntityManagerEventSubscriber
public function support(EntityInterface $entity)
{
return $entity instanceof Block;
return $entity instanceof Page;
}
public function onPreUpdate(EntityManagerEvent $event)
@ -34,21 +36,21 @@ class BlockEventSubscriber extends EntityManagerEventSubscriber
return;
}
$block = $event->getEntity();
foreach ($event->getEntity()->getBlocks() as $block) {
if ($block instanceof FileBlock) {
if ($block->getValue() instanceof UploadedFile) {
$directory = 'uploads/page/block';
if (!$block->getValue() instanceof UploadedFile) {
return;
}
$directory = 'uploads/page/block';
$fileUpload->handleForm(
$block->getValue(),
$directory,
function ($filename) use ($block) {
$block->setValue($directory.'/'.$filename);
$this->fileUpload->handleForm(
$block->getValue(),
$directory,
function ($filename) use ($block, $directory) {
$block->setValue($directory.'/'.$filename);
}
);
}
}
);
}
}
public function onPreCreate(EntityManagerEvent $event)

View File

@ -0,0 +1,36 @@
<?php
namespace App\Core\Form\Site\Page;
use App\Core\Entity\Site\Page\Block;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use App\Core\Entity\Site\Page\FileBlock;
class FileBlockType extends TextBlockType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add(
'value',
FileType::class,
array_merge([
'required' => false,
'label' => false,
], $options['options']),
);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => FileBlock::class,
'options' => [],
]);
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace App\Core\Form\Site\Page;
use App\Core\Entity\Site\Page\Block;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use App\Core\Entity\Site\Page\FileBlock;
use Symfony\Component\Validator\Constraints\Image;
class ImageBlockType extends FileBlockType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add(
'value',
FileType::class,
array_merge([
'required' => false,
'label' => false,
'constraints' => [
new Image(),
],
], $options['options']),
);
}
}

View File

@ -74,7 +74,7 @@ class PostAdminController extends AdminController
$fileUpload->handleForm(
$form->get('image')->getData(),
$directory,
function ($filename) use ($entity) {
function ($filename) use ($entity, $directory) {
$entity->setImage($directory.'/'.$filename);
}
);

View File

@ -9,6 +9,8 @@ use App\Core\Form\Site\Page\TextareaBlockType;
use App\Core\Form\Site\Page\TextBlockType;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Form\FormBuilderInterface;
use App\Core\Entity\Site\Page\FileBlock;
use App\Core\Form\Site\Page\ImageBlockType;
/**
* @ORM\Entity
@ -46,19 +48,19 @@ class SimplePage extends Page
]
);
// $builder->add(
// 'file',
// FileBlockType::class,
// [
// 'label' => 'Fichier',
// 'options' => [
// 'attr' => [
// ],
// 'constraints' => [
// ],
// ],
// ]
// );
$builder->add(
'image',
ImageBlockType::class,
[
'label' => 'Image',
'options' => [
'attr' => [
],
'constraints' => [
],
],
]
);
}
public function setTitle(Block $block)
@ -81,13 +83,13 @@ class SimplePage extends Page
return $this->getBlock('content');
}
public function setFile(Block $block)
public function setImage(Block $block)
{
return $this->setBlock($block);
}
public function getFile()
public function getImage()
{
return $this->getBlock('file');
return $this->getBlock('image', FileBlock::class);
}
}

View File

@ -50,6 +50,12 @@
<pre>{{ _page.content.value }}</pre>
{% set image = _page.image.value %}
{% if image %}
<img src="{{ asset(image) }}" alt="">
{% endif %}
<ul>
<li>
Node : {{ _node.label }}