add event when a page is edited in order to add specific options in the form

This commit is contained in:
Simon Vieille 2021-08-17 09:12:43 +02:00
parent a7c1824f23
commit 756c0479b4
4 changed files with 57 additions and 5 deletions

View file

@ -15,6 +15,8 @@ use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
use App\Core\Event\Page\PageEditEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class PageAdminController extends CrudController class PageAdminController extends CrudController
{ {
@ -50,12 +52,17 @@ class PageAdminController extends CrudController
EntityManager $entityManager, EntityManager $entityManager,
RepositoryQuery $repositoryQuery, RepositoryQuery $repositoryQuery,
PageLocator $pageLocator, PageLocator $pageLocator,
EventDispatcherInterface $eventDispatcher,
Request $request Request $request
): Response { ): Response {
$entity = $repositoryQuery->filterById($entity)->findOne(); $entity = $repositoryQuery->filterById($entity)->findOne();
$event = new PageEditEvent($entity);
$eventDispatcher->dispatch($event, PageEditEvent::FORM_INIT_EVENT);
$this->getConfiguration()->setFormOptions('edit', [ $this->getConfiguration()->setFormOptions('edit', [
'pageConfiguration' => $pageLocator->getPage(get_class($entity)), 'page_configuration' => $pageLocator->getPage(get_class($entity)),
'page_builder_options' => $event->getPageBuilderOptions(),
]); ]);
return $this->doEdit($entity, $entityManager, $request); return $this->doEdit($entity, $entityManager, $request);

View file

@ -140,7 +140,7 @@ class Page implements EntityInterface
return $this; return $this;
} }
public function buildForm(FormBuilderInterface $builder) public function buildForm(FormBuilderInterface $builder, array $options)
{ {
} }

View file

@ -0,0 +1,44 @@
<?php
namespace App\Core\Event\Page;
use Symfony\Contracts\EventDispatcher\Event;
use App\Core\Entity\Site\Page\Page;
/**
* class PageEditEvent.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class PageEditEvent extends Event
{
const FORM_INIT_EVENT = 'page_edit_event.form_init';
protected Page $page;
protected array $pageBuilderOptions = [];
public function __construct(Page $page)
{
$this->page = $page;
}
public function getPage()
{
return $this->page;
}
public function getPageBuilderOptions(): array
{
return $this->pageBuilderOptions;
}
public function addPageBuilderOptions(array $options): self
{
$this->pageBuilderOptions = array_merge(
$this->pageBuilderOptions,
$options
);
return $this;
}
}

View file

@ -105,7 +105,7 @@ class PageType extends AbstractType
'choices' => call_user_func(function () use ($options) { 'choices' => call_user_func(function () use ($options) {
$choices = []; $choices = [];
foreach ($options['pageConfiguration']->getTemplates() as $template) { foreach ($options['page_configuration']->getTemplates() as $template) {
$choices[$template['name']] = $template['file']; $choices[$template['name']] = $template['file'];
} }
@ -119,14 +119,15 @@ class PageType extends AbstractType
] ]
); );
$builder->getData()->buildForm($builder); $builder->getData()->buildForm($builder, $options['page_builder_options']);
} }
public function configureOptions(OptionsResolver $resolver) public function configureOptions(OptionsResolver $resolver)
{ {
$resolver->setDefaults([ $resolver->setDefaults([
'data_class' => Page::class, 'data_class' => Page::class,
'pageConfiguration' => null, 'page_configuration' => null,
'page_builder_options' => [],
]); ]);
} }
} }