add file block type for page
This commit is contained in:
parent
b32b292c22
commit
36168cfb0f
|
@ -4,4 +4,4 @@ app:
|
||||||
App\Entity\Page\SimplePage:
|
App\Entity\Page\SimplePage:
|
||||||
name: 'Page simple'
|
name: 'Page simple'
|
||||||
templates:
|
templates:
|
||||||
- {name: "Template 1", file: "site/page/simple/page.html.twig"}
|
- {name: "Template 1", file: "page/simple/page.html.twig"}
|
||||||
|
|
|
@ -8,6 +8,8 @@ use Doctrine\ORM\Mapping as ORM;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ORM\Entity(repositoryClass=BlockRepository::class)
|
* @ORM\Entity(repositoryClass=BlockRepository::class)
|
||||||
|
* @ORM\DiscriminatorColumn(name="class_key", type="string")
|
||||||
|
* @ORM\InheritanceType("SINGLE_TABLE")
|
||||||
* @ORM\HasLifecycleCallbacks
|
* @ORM\HasLifecycleCallbacks
|
||||||
*/
|
*/
|
||||||
class Block
|
class Block
|
||||||
|
@ -54,7 +56,7 @@ class Block
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getValue(): ?string
|
public function getValue()
|
||||||
{
|
{
|
||||||
return $this->value;
|
return $this->value;
|
||||||
}
|
}
|
||||||
|
|
36
core/Entity/Site/Page/FileBlock.php
Normal file
36
core/Entity/Site/Page/FileBlock.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -137,7 +137,7 @@ class Page implements EntityInterface
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getBlock($name)
|
public function getBlock($name, string $className = null)
|
||||||
{
|
{
|
||||||
foreach ($this->getBlocks() as $block) {
|
foreach ($this->getBlocks() as $block) {
|
||||||
if ($block->getName() === $name) {
|
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->setName($name);
|
||||||
$block->setPage($this);
|
$block->setPage($this);
|
||||||
|
|
||||||
|
|
14
core/Entity/Site/Page/TextBlock.php
Normal file
14
core/Entity/Site/Page/TextBlock.php
Normal 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
|
||||||
|
{
|
||||||
|
}
|
|
@ -7,6 +7,8 @@ use App\Core\Entity\Site\Page\Block;
|
||||||
use App\Core\Event\EntityManager\EntityManagerEvent;
|
use App\Core\Event\EntityManager\EntityManagerEvent;
|
||||||
use App\Core\EventSuscriber\EntityManagerEventSubscriber;
|
use App\Core\EventSuscriber\EntityManagerEventSubscriber;
|
||||||
use App\Core\Form\FileUploadHandler;
|
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;
|
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -25,7 +27,7 @@ class BlockEventSubscriber extends EntityManagerEventSubscriber
|
||||||
|
|
||||||
public function support(EntityInterface $entity)
|
public function support(EntityInterface $entity)
|
||||||
{
|
{
|
||||||
return $entity instanceof Block;
|
return $entity instanceof Page;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onPreUpdate(EntityManagerEvent $event)
|
public function onPreUpdate(EntityManagerEvent $event)
|
||||||
|
@ -34,21 +36,21 @@ class BlockEventSubscriber extends EntityManagerEventSubscriber
|
||||||
return;
|
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) {
|
$this->fileUpload->handleForm(
|
||||||
return;
|
$block->getValue(),
|
||||||
}
|
$directory,
|
||||||
|
function ($filename) use ($block, $directory) {
|
||||||
$directory = 'uploads/page/block';
|
$block->setValue($directory.'/'.$filename);
|
||||||
|
}
|
||||||
$fileUpload->handleForm(
|
);
|
||||||
$block->getValue(),
|
}
|
||||||
$directory,
|
|
||||||
function ($filename) use ($block) {
|
|
||||||
$block->setValue($directory.'/'.$filename);
|
|
||||||
}
|
}
|
||||||
);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onPreCreate(EntityManagerEvent $event)
|
public function onPreCreate(EntityManagerEvent $event)
|
||||||
|
|
36
core/Form/Site/Page/FileBlockType.php
Normal file
36
core/Form/Site/Page/FileBlockType.php
Normal 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' => [],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
32
core/Form/Site/Page/ImageBlockType.php
Normal file
32
core/Form/Site/Page/ImageBlockType.php
Normal 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']),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -74,7 +74,7 @@ class PostAdminController extends AdminController
|
||||||
$fileUpload->handleForm(
|
$fileUpload->handleForm(
|
||||||
$form->get('image')->getData(),
|
$form->get('image')->getData(),
|
||||||
$directory,
|
$directory,
|
||||||
function ($filename) use ($entity) {
|
function ($filename) use ($entity, $directory) {
|
||||||
$entity->setImage($directory.'/'.$filename);
|
$entity->setImage($directory.'/'.$filename);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
|
@ -9,6 +9,8 @@ use App\Core\Form\Site\Page\TextareaBlockType;
|
||||||
use App\Core\Form\Site\Page\TextBlockType;
|
use App\Core\Form\Site\Page\TextBlockType;
|
||||||
use Doctrine\ORM\Mapping as ORM;
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
use Symfony\Component\Form\FormBuilderInterface;
|
use Symfony\Component\Form\FormBuilderInterface;
|
||||||
|
use App\Core\Entity\Site\Page\FileBlock;
|
||||||
|
use App\Core\Form\Site\Page\ImageBlockType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ORM\Entity
|
* @ORM\Entity
|
||||||
|
@ -46,19 +48,19 @@ class SimplePage extends Page
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
// $builder->add(
|
$builder->add(
|
||||||
// 'file',
|
'image',
|
||||||
// FileBlockType::class,
|
ImageBlockType::class,
|
||||||
// [
|
[
|
||||||
// 'label' => 'Fichier',
|
'label' => 'Image',
|
||||||
// 'options' => [
|
'options' => [
|
||||||
// 'attr' => [
|
'attr' => [
|
||||||
// ],
|
],
|
||||||
// 'constraints' => [
|
'constraints' => [
|
||||||
// ],
|
],
|
||||||
// ],
|
],
|
||||||
// ]
|
]
|
||||||
// );
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setTitle(Block $block)
|
public function setTitle(Block $block)
|
||||||
|
@ -81,13 +83,13 @@ class SimplePage extends Page
|
||||||
return $this->getBlock('content');
|
return $this->getBlock('content');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setFile(Block $block)
|
public function setImage(Block $block)
|
||||||
{
|
{
|
||||||
return $this->setBlock($block);
|
return $this->setBlock($block);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getFile()
|
public function getImage()
|
||||||
{
|
{
|
||||||
return $this->getBlock('file');
|
return $this->getBlock('image', FileBlock::class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,6 +50,12 @@
|
||||||
|
|
||||||
<pre>{{ _page.content.value }}</pre>
|
<pre>{{ _page.content.value }}</pre>
|
||||||
|
|
||||||
|
{% set image = _page.image.value %}
|
||||||
|
|
||||||
|
{% if image %}
|
||||||
|
<img src="{{ asset(image) }}" alt="">
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
<ul>
|
<ul>
|
||||||
<li>
|
<li>
|
||||||
Node : {{ _node.label }}
|
Node : {{ _node.label }}
|
Loading…
Reference in a new issue