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\Session\Session;
use Symfony\Component\Routing\Annotation\Route;
use App\Core\Event\Page\PageEditEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class PageAdminController extends CrudController
{
@ -50,12 +52,17 @@ class PageAdminController extends CrudController
EntityManager $entityManager,
RepositoryQuery $repositoryQuery,
PageLocator $pageLocator,
EventDispatcherInterface $eventDispatcher,
Request $request
): Response {
$entity = $repositoryQuery->filterById($entity)->findOne();
$event = new PageEditEvent($entity);
$eventDispatcher->dispatch($event, PageEditEvent::FORM_INIT_EVENT);
$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);

View File

@ -140,7 +140,7 @@ class Page implements EntityInterface
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 = [];
foreach ($options['pageConfiguration']->getTemplates() as $template) {
foreach ($options['page_configuration']->getTemplates() as $template) {
$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)
{
$resolver->setDefaults([
'data_class' => Page::class,
'pageConfiguration' => null,
'page_configuration' => null,
'page_builder_options' => [],
]);
}
}