backports murph-skeleton

This commit is contained in:
Simon Vieille 2021-05-12 15:24:01 +02:00
parent 77c234987d
commit aca3b7d676
3 changed files with 21 additions and 5 deletions

View file

@ -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();
$form = $this->createForm($configuration->getForm('new'), $entity);
@ -52,6 +52,10 @@ abstract class CrudController extends AdminController
$form->handleRequest($request);
if ($form->isValid()) {
if ($beforeCreate !== null) {
call_user_func_array($beforeCreate, [$entity, $form, $request]);
}
$entityManager->create($entity);
$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();
$form = $this->createForm($configuration->getForm('edit'), $entity);
@ -88,6 +92,10 @@ abstract class CrudController extends AdminController
$form->handleRequest($request);
if ($form->isValid()) {
if ($beforeUpdate !== null) {
call_user_func_array($beforeUpdate, [$entity, $form, $request]);
}
$entityManager->update($entity);
$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();
if ($this->isCsrfTokenValid('delete'.$entity->getId(), $request->request->get('_token'))) {
if ($beforeDelete !== null) {
call_user_func($beforeDelete, $entity);
}
$entityManager->delete($entity);
$this->addFlash('success', 'The data has been removed.');

View file

@ -17,6 +17,7 @@ abstract class Field
public function buildView(Environment $twig, $entity, array $options)
{
return $twig->render($this->getView($options), [
'entity' => $entity,
'value' => $this->getValue($entity, $options),
'options' => $options,
]);
@ -53,7 +54,7 @@ abstract class Field
} elseif (null !== $options['property_builder']) {
$value = call_user_func($options['property_builder'], $entity, $options);
} else {
throw new CrudConfigurationException('Unable to get the value. One of "property" and "property_builder" is required.');
$value = null;
}
return $value;

View file

@ -1 +1,4 @@
{{ value|date(options.format) }}
<span class="btn btn-sm btn-light">
<span class="fa fa-calendar-alt text-black-50 mr-1"></span>
{{ value|date(options.format) }}
</span>