Skip to content

Instantly share code, notes, and snippets.

@jbinfo
Created August 3, 2024 10:25
Show Gist options
  • Save jbinfo/06fc2b75efcb8489951632190529e57d to your computer and use it in GitHub Desktop.
Save jbinfo/06fc2b75efcb8489951632190529e57d to your computer and use it in GitHub Desktop.
Symfony EasyAdmin customize form with events
<?php
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
class BaseAdminCrudController extends AbstractCrudController
{
public function edit(AdminContext $context)
{
$event = new BeforeCrudActionEvent($context);
$this->container->get('event_dispatcher')->dispatch($event);
if ($event->isPropagationStopped()) {
return $event->getResponse();
}
if (!$this->isGranted(Permission::EA_EXECUTE_ACTION, ['action' => Action::EDIT, 'entity' => $context->getEntity()])) {
throw new ForbiddenActionException($context);
}
if (!$context->getEntity()->isAccessible()) {
throw new InsufficientEntityPermissionException($context);
}
$this->container->get(EntityFactory::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_EDIT)));
$context->getCrud()->setFieldAssets($this->getFieldAssets($context->getEntity()->getFields()));
$this->container->get(EntityFactory::class)->processActions($context->getEntity(), $context->getCrud()->getActionsConfig());
$entityInstance = $context->getEntity()->getInstance();
if ($context->getRequest()->isXmlHttpRequest()) {
if ('PATCH' !== $context->getRequest()->getMethod()) {
throw new MethodNotAllowedHttpException(['PATCH']);
}
if (!$this->isCsrfTokenValid(BooleanField::CSRF_TOKEN_NAME, $context->getRequest()->query->get('csrfToken'))) {
if (class_exists(InvalidCsrfTokenException::class)) {
throw new InvalidCsrfTokenException();
} else {
return new Response('Invalid CSRF token.', 400);
}
}
$fieldName = $context->getRequest()->query->get('fieldName');
$newValue = 'true' === mb_strtolower($context->getRequest()->query->get('newValue'));
try {
$event = $this->ajaxEdit($context->getEntity(), $fieldName, $newValue);
} catch (\Exception $e) {
throw new BadRequestHttpException($e->getMessage());
}
if ($event->isPropagationStopped()) {
return $event->getResponse();
}
return new Response($newValue ? '1' : '0');
}
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/
$editFormBuilder = $this->createEditFormBuilder($context->getEntity(), $context->getCrud()->getEditFormOptions(), $context);
$this->container->get('event_dispatcher')->dispatch(new YourClassFormEvent($editFormBuilder));
$editForm = $editFormBuilder->getForm();
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/
$editForm->handleRequest($context->getRequest());
if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->processUploadedFiles($editForm);
$event = new BeforeEntityUpdatedEvent($entityInstance);
$this->container->get('event_dispatcher')->dispatch($event);
$entityInstance = $event->getEntityInstance();
$this->updateEntity($this->container->get('doctrine')->getManagerForClass($context->getEntity()->getFqcn()), $entityInstance);
$this->container->get('event_dispatcher')->dispatch(new AfterEntityUpdatedEvent($entityInstance));
return $this->getRedirectResponseAfterSave($context, Action::EDIT);
}
$responseParameters = $this->configureResponseParameters(KeyValueStore::new([
'pageName' => Crud::PAGE_EDIT,
'templateName' => 'crud/edit',
'edit_form' => $editForm,
'entity' => $context->getEntity(),
]));
$event = new AfterCrudActionEvent($context, $responseParameters);
$this->container->get('event_dispatcher')->dispatch($event);
if ($event->isPropagationStopped()) {
return $event->getResponse();
}
return $responseParameters;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment