add callback before create/update/delete
This commit is contained in:
parent
329405801f
commit
22e2c53e9d
|
@ -43,7 +43,7 @@ abstract class CrudController extends AdminController
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function doNew(EntityInterface $entity, EntityManager $entityManager, Request $request): Response
|
protected function doNew(EntityInterface $entity, EntityManager $entityManager, Request $request, callable $beforeCreate = null): Response
|
||||||
{
|
{
|
||||||
$configuration = $this->getConfiguration();
|
$configuration = $this->getConfiguration();
|
||||||
$form = $this->createForm($configuration->getForm('new'), $entity);
|
$form = $this->createForm($configuration->getForm('new'), $entity);
|
||||||
|
@ -52,6 +52,10 @@ abstract class CrudController extends AdminController
|
||||||
$form->handleRequest($request);
|
$form->handleRequest($request);
|
||||||
|
|
||||||
if ($form->isValid()) {
|
if ($form->isValid()) {
|
||||||
|
if ($beforeCreate !== null) {
|
||||||
|
call_user_func_array($beforeCreate, [$entity, $form, $request]);
|
||||||
|
}
|
||||||
|
|
||||||
$entityManager->create($entity);
|
$entityManager->create($entity);
|
||||||
$this->addFlash('success', 'The data has been saved.');
|
$this->addFlash('success', 'The data has been saved.');
|
||||||
|
|
||||||
|
@ -79,7 +83,7 @@ abstract class CrudController extends AdminController
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function doEdit(EntityInterface $entity, EntityManager $entityManager, Request $request): Response
|
protected function doEdit(EntityInterface $entity, EntityManager $entityManager, Request $request, callable $beforeUpdate = null): Response
|
||||||
{
|
{
|
||||||
$configuration = $this->getConfiguration();
|
$configuration = $this->getConfiguration();
|
||||||
$form = $this->createForm($configuration->getForm('edit'), $entity);
|
$form = $this->createForm($configuration->getForm('edit'), $entity);
|
||||||
|
@ -88,6 +92,10 @@ abstract class CrudController extends AdminController
|
||||||
$form->handleRequest($request);
|
$form->handleRequest($request);
|
||||||
|
|
||||||
if ($form->isValid()) {
|
if ($form->isValid()) {
|
||||||
|
if ($beforeUpdate !== null) {
|
||||||
|
call_user_func_array($beforeUpdate, [$entity, $form, $request]);
|
||||||
|
}
|
||||||
|
|
||||||
$entityManager->update($entity);
|
$entityManager->update($entity);
|
||||||
$this->addFlash('success', 'The data has been saved.');
|
$this->addFlash('success', 'The data has been saved.');
|
||||||
|
|
||||||
|
@ -105,11 +113,15 @@ abstract class CrudController extends AdminController
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function doDelete(EntityInterface $entity, EntityManager $entityManager, Request $request): Response
|
protected function doDelete(EntityInterface $entity, EntityManager $entityManager, Request $request, callable $beforeDelete = null): Response
|
||||||
{
|
{
|
||||||
$configuration = $this->getConfiguration();
|
$configuration = $this->getConfiguration();
|
||||||
|
|
||||||
if ($this->isCsrfTokenValid('delete'.$entity->getId(), $request->request->get('_token'))) {
|
if ($this->isCsrfTokenValid('delete'.$entity->getId(), $request->request->get('_token'))) {
|
||||||
|
if ($beforeDelete !== null) {
|
||||||
|
call_user_func($beforeDelete, $entity);
|
||||||
|
}
|
||||||
|
|
||||||
$entityManager->delete($entity);
|
$entityManager->delete($entity);
|
||||||
|
|
||||||
$this->addFlash('success', 'The data has been removed.');
|
$this->addFlash('success', 'The data has been removed.');
|
||||||
|
|
Loading…
Reference in a new issue