add ogImage in page
This commit is contained in:
parent
95a2d0a894
commit
e3b463507d
|
@ -10,6 +10,7 @@ use Doctrine\Common\Collections\ArrayCollection;
|
||||||
use Doctrine\Common\Collections\Collection;
|
use Doctrine\Common\Collections\Collection;
|
||||||
use Doctrine\ORM\Mapping as ORM;
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
use Symfony\Component\Form\FormBuilderInterface;
|
use Symfony\Component\Form\FormBuilderInterface;
|
||||||
|
use Symfony\Component\HttpFoundation\File\File;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ORM\Entity(repositoryClass=PageRepository::class)
|
* @ORM\Entity(repositoryClass=PageRepository::class)
|
||||||
|
@ -26,47 +27,52 @@ class Page implements EntityInterface
|
||||||
* @ORM\GeneratedValue
|
* @ORM\GeneratedValue
|
||||||
* @ORM\Column(type="integer")
|
* @ORM\Column(type="integer")
|
||||||
*/
|
*/
|
||||||
private $id;
|
protected $id;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ORM\Column(type="string", length=255)
|
* @ORM\Column(type="string", length=255)
|
||||||
*/
|
*/
|
||||||
private $name;
|
protected $name;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ORM\Column(type="string", length=255, nullable=true)
|
* @ORM\Column(type="string", length=255, nullable=true)
|
||||||
*/
|
*/
|
||||||
private $template;
|
protected $template;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ORM\OneToMany(targetEntity=Block::class, mappedBy="page", cascade={"persist"})
|
* @ORM\OneToMany(targetEntity=Block::class, mappedBy="page", cascade={"persist"})
|
||||||
*/
|
*/
|
||||||
private $blocks;
|
protected $blocks;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ORM\Column(type="string", length=255, nullable=true)
|
* @ORM\Column(type="string", length=255, nullable=true)
|
||||||
*/
|
*/
|
||||||
private $metaTitle;
|
protected $metaTitle;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ORM\Column(type="string", length=255, nullable=true)
|
* @ORM\Column(type="string", length=255, nullable=true)
|
||||||
*/
|
*/
|
||||||
private $metaDescrition;
|
protected $metaDescrition;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ORM\Column(type="string", length=255, nullable=true)
|
* @ORM\Column(type="string", length=255, nullable=true)
|
||||||
*/
|
*/
|
||||||
private $ogTitle;
|
protected $ogTitle;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ORM\Column(type="string", length=255, nullable=true)
|
* @ORM\Column(type="string", length=255, nullable=true)
|
||||||
*/
|
*/
|
||||||
private $ogDescription;
|
protected $ogDescription;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\Column(type="string", length=255, nullable=true)
|
||||||
|
*/
|
||||||
|
protected $ogImage;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ORM\OneToMany(targetEntity=Node::class, mappedBy="page")
|
* @ORM\OneToMany(targetEntity=Node::class, mappedBy="page")
|
||||||
*/
|
*/
|
||||||
private $nodes;
|
protected $nodes;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
|
@ -221,6 +227,26 @@ class Page implements EntityInterface
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getOgImage()
|
||||||
|
{
|
||||||
|
if (is_string($this->ogImage)) {
|
||||||
|
if (file_exists($this->ogImage)) {
|
||||||
|
return new File($this->ogImage);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->ogImage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setOgImage($ogImage): self
|
||||||
|
{
|
||||||
|
$this->ogImage = $ogImage;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Collection|Node[]
|
* @return Collection|Node[]
|
||||||
*/
|
*/
|
||||||
|
|
56
core/EventSuscriber/Site/Page/PageEventSubscriber.php
Normal file
56
core/EventSuscriber/Site/Page/PageEventSubscriber.php
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Core\EventSuscriber\Site\Page;
|
||||||
|
|
||||||
|
use App\Core\Entity\EntityInterface;
|
||||||
|
use App\Core\Entity\Site\Page\Page;
|
||||||
|
use App\Core\Event\EntityManager\EntityManagerEvent;
|
||||||
|
use App\Core\EventSuscriber\EntityManagerEventSubscriber;
|
||||||
|
use App\Core\Form\FileUploadHandler;
|
||||||
|
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* class PageEventSubscriber.
|
||||||
|
*
|
||||||
|
* @author Simon Vieille <simon@deblan.fr>
|
||||||
|
*/
|
||||||
|
class PageEventSubscriber extends EntityManagerEventSubscriber
|
||||||
|
{
|
||||||
|
protected FileUploadHandler $fileUpload;
|
||||||
|
|
||||||
|
public function __construct(FileUploadHandler $fileUpload)
|
||||||
|
{
|
||||||
|
$this->fileUpload = $fileUpload;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function support(EntityInterface $entity)
|
||||||
|
{
|
||||||
|
return $entity instanceof Page;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onPreUpdate(EntityManagerEvent $event)
|
||||||
|
{
|
||||||
|
if (!$this->support($event->getEntity())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$page = $event->getEntity();
|
||||||
|
|
||||||
|
if ($page->getOgImage() instanceof UploadedFile) {
|
||||||
|
$directory = 'uploads/page/ogImage';
|
||||||
|
|
||||||
|
$this->fileUpload->handleForm(
|
||||||
|
$page->getOgImage(),
|
||||||
|
$directory,
|
||||||
|
function ($filename) use ($page, $directory) {
|
||||||
|
$page->setOgImage($directory.'/'.$filename);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onPreCreate(EntityManagerEvent $event)
|
||||||
|
{
|
||||||
|
return $this->onPreUpdate($event);
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,6 +9,8 @@ use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||||
use Symfony\Component\Form\FormBuilderInterface;
|
use Symfony\Component\Form\FormBuilderInterface;
|
||||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||||
use Symfony\Component\Validator\Constraints\NotBlank;
|
use Symfony\Component\Validator\Constraints\NotBlank;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\FileType;
|
||||||
|
use Symfony\Component\Validator\Constraints\Image;
|
||||||
|
|
||||||
class PageType extends AbstractType
|
class PageType extends AbstractType
|
||||||
{
|
{
|
||||||
|
@ -80,6 +82,20 @@ class PageType extends AbstractType
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$builder->add(
|
||||||
|
'ogImage',
|
||||||
|
FileType::class,
|
||||||
|
[
|
||||||
|
'label' => 'Image',
|
||||||
|
'required' => false,
|
||||||
|
'attr' => [
|
||||||
|
],
|
||||||
|
'constraints' => [
|
||||||
|
new Image(),
|
||||||
|
],
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
$builder->add(
|
$builder->add(
|
||||||
'template',
|
'template',
|
||||||
ChoiceType::class,
|
ChoiceType::class,
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
{% endset %}
|
{% endset %}
|
||||||
|
|
||||||
{% set formOpenGraph %}
|
{% set formOpenGraph %}
|
||||||
{% for item in ['ogTitle', 'ogDescription'] %}
|
{% for item in ['ogTitle', 'ogDescription', 'ogImage'] %}
|
||||||
{{ form_row(form[item]) }}
|
{{ form_row(form[item]) }}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
{% endset %}
|
{% endset %}
|
||||||
|
|
Loading…
Reference in a new issue