mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
default crud new function
This commit is contained in:
parent
f85462ac40
commit
b402eabeb9
@ -132,6 +132,11 @@ class CRUDController extends AbstractController
|
||||
return $this->formEditAction('edit', $request, $id);
|
||||
}
|
||||
|
||||
public function new(Request $request): Response
|
||||
{
|
||||
return $this->formCreateAction('new', $request);
|
||||
}
|
||||
|
||||
public function view(Request $request, $id): Response
|
||||
{
|
||||
return $this->viewAction('view', $request, $id);
|
||||
@ -218,7 +223,7 @@ class CRUDController extends AbstractController
|
||||
|
||||
protected function formCreateAction($action, Request $request, $formClass = null): Response
|
||||
{
|
||||
$entity = $this->createEntity($request);
|
||||
$entity = $this->createEntity($action, $request);
|
||||
|
||||
$this->checkACL($action, $entity);
|
||||
|
||||
@ -240,13 +245,13 @@ class CRUDController extends AbstractController
|
||||
$this->getPaginatorFactory();
|
||||
$this->addFlash('succes', $this->generateFormSuccessMessage($action, $entity));
|
||||
|
||||
$result = $this->onBeforRedirect($action, $entity, $form, $request);
|
||||
$result = $this->onBeforeRedirect($action, $entity, $form, $request);
|
||||
|
||||
if ($result instanceof Response) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
return $this->redirectToRoute('chill_crud_'.$this->get, ['id' => $entity->getId()]);
|
||||
return $this->redirectToRoute('chill_crud_'.$this->getCrudName().'_view', ['id' => $entity->getId()]);
|
||||
|
||||
} elseif ($form->isSubmitted()) {
|
||||
$this->addFlash('error', $this->generateFormErrorMessage($action, $form));
|
||||
@ -254,11 +259,12 @@ class CRUDController extends AbstractController
|
||||
|
||||
$defaultTemplateParameters = [
|
||||
'form' => $form->createView(),
|
||||
'entity' => $entity
|
||||
'entity' => $entity,
|
||||
'crud_name' => $this->getCrudName()
|
||||
];
|
||||
|
||||
return $this->render(
|
||||
$this->getTemplateFor($action),
|
||||
$this->getTemplateFor($action, $entity, $request),
|
||||
$this->generateTemplateParameter($action, $entity, $request, $defaultTemplateParameters)
|
||||
);
|
||||
}
|
||||
@ -289,13 +295,17 @@ class CRUDController extends AbstractController
|
||||
return $this->crudConfig['name'];
|
||||
}
|
||||
|
||||
protected function checkACL($entity, $action)
|
||||
protected function checkACL($action, $entity)
|
||||
{
|
||||
$this->denyAccessUnlessGranted($this->getRoleFor($action), $entity);
|
||||
}
|
||||
|
||||
protected function getRoleFor($action)
|
||||
{
|
||||
if (NULL !== ($this->getActionConfig($action)['role'])) {
|
||||
return $this->getActionConfig($action)['role'];
|
||||
}
|
||||
|
||||
return $this->buildDefaultRole($action);
|
||||
}
|
||||
|
||||
@ -373,16 +383,17 @@ class CRUDController extends AbstractController
|
||||
return $defaultTemplateParameters;
|
||||
}
|
||||
|
||||
protected function createEntity(Request $request): object
|
||||
protected function createEntity($action, Request $request): object
|
||||
{
|
||||
$type = $this->getEntityClass();
|
||||
return new $type;
|
||||
|
||||
return new $type;
|
||||
}
|
||||
|
||||
protected function getTemplateFor($action, $entity, Request $request)
|
||||
{
|
||||
if (!empty($this->crudConfig[$action]['template'])) {
|
||||
return $this->crudConfig[$action]['template'];
|
||||
if ($this->hasCustomTemplate($action, $entity, $request)) {
|
||||
return $this->getActionConfig($action)['template'];
|
||||
}
|
||||
|
||||
switch ($action) {
|
||||
@ -399,6 +410,11 @@ class CRUDController extends AbstractController
|
||||
}
|
||||
}
|
||||
|
||||
protected function hasCustomTemplate($action, $entity, Request $request): bool
|
||||
{
|
||||
return !empty($this->getActionConfig($action)['template']);
|
||||
}
|
||||
|
||||
protected function onPreFlush(string $action, $entity, FormInterface $form, Request $request)
|
||||
{
|
||||
}
|
||||
@ -432,6 +448,11 @@ class CRUDController extends AbstractController
|
||||
protected function onBeforeRedirect(string $action, $entity, FormInterface $form, Request $request)
|
||||
{
|
||||
}
|
||||
|
||||
protected function getActionConfig(string $action)
|
||||
{
|
||||
return $this->crudConfig['actions'][$action];
|
||||
}
|
||||
|
||||
protected function getPaginatorFactory(): PaginatorFactory
|
||||
{
|
||||
|
@ -67,10 +67,13 @@ class CRUDRoutesLoader
|
||||
$defaults = [
|
||||
'_controller' => $action['controller'] ?? $config['controller'].'::'.$name
|
||||
];
|
||||
|
||||
if ($action === 'index') {
|
||||
|
||||
if ($name === 'index') {
|
||||
$path = "{_locale}".$config['base_path'];
|
||||
$route = new Route($path, $defaults);
|
||||
} elseif ($name === 'new') {
|
||||
$path = "{_locale}".$config['base_path'].'/'.$name;
|
||||
$route = new Route($path, $defaults);
|
||||
} else {
|
||||
$path = "{_locale}".$config['base_path'].($action['path'] ?? '/{id}/'.$name);
|
||||
$requirements = $action['requirements'] ?? [
|
||||
|
@ -135,12 +135,12 @@ class Configuration implements ConfigurationInterface
|
||||
->arrayPrototype()
|
||||
->children()
|
||||
->scalarNode('controller')
|
||||
->defaultValue('')
|
||||
->defaultNull()
|
||||
->info('the method name to call in the route. Will be set to the action name if left empty.')
|
||||
->example("'MyBundle\Controller\MyCrudController::action'")
|
||||
->end()
|
||||
->scalarNode('path')
|
||||
->defaultValue('')
|
||||
->defaultNull()
|
||||
->info('the path that will be **appended** after the base path. Do not forget to add '
|
||||
. 'arguments for the method. Will be set to the action name, including an `{id}` '
|
||||
. 'parameter if left empty.')
|
||||
@ -150,6 +150,10 @@ class Configuration implements ConfigurationInterface
|
||||
->ignoreExtraKeys(false)
|
||||
->info('the requirements for the route. Will be set to `[ \'id\' => \'\d+\' ]` if left empty.')
|
||||
->end()
|
||||
->scalarNode('role')
|
||||
->defaultNull()
|
||||
->info('the role that will be required for this action. Override option `base_role`')
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
|
30
Resources/views/CRUD/_new_content.html.twig
Normal file
30
Resources/views/CRUD/_new_content.html.twig
Normal file
@ -0,0 +1,30 @@
|
||||
<div class="{% block crud_content_main_div_class %}grid-10 centered{% endblock %}">
|
||||
{% block crud_content_header %}
|
||||
<h1>{{ 'crud.%crud_name%.title_new'|trans({'%crud_name%' : crud_name }) }}</h1>
|
||||
{% endblock crud_content_header %}
|
||||
|
||||
{% block crud_content_form %}
|
||||
{{ form_start(form) }}
|
||||
|
||||
{% block crud_content_form_rows %}
|
||||
{% for f in form if f.vars.name != 'submit' %}
|
||||
{{ form_row(f) }}
|
||||
{% endfor %}
|
||||
{% endblock crud_content_form_rows %}
|
||||
|
||||
{% block crud_content_form_actions %}
|
||||
<ul class="record_actions sticky-form-buttons">
|
||||
{% block content_form_actions_back %}
|
||||
<li class="cancel">
|
||||
<a class="sc-button bt-cancel" href="{{ chill_return_path_or('chill_crud_'~crud_name~'_index') }}">
|
||||
{{ 'Cancel'|trans }}
|
||||
</a>
|
||||
</li>
|
||||
{% endblock %}
|
||||
<li>{{ form_widget(form.submit, { 'attr': { 'class': 'sc-button bt-new'} } ) }}</li>
|
||||
</ul>
|
||||
{% endblock %}
|
||||
|
||||
{{ form_end(form) }}
|
||||
{% endblock %}
|
||||
</div>
|
1
Resources/views/CRUD/_new_title.html.twig
Normal file
1
Resources/views/CRUD/_new_title.html.twig
Normal file
@ -0,0 +1 @@
|
||||
{{ 'crud.%crud_name%.title_new'|trans({'%crud_name%' : crud_name }) }}
|
10
Resources/views/CRUD/new.html.twig
Normal file
10
Resources/views/CRUD/new.html.twig
Normal file
@ -0,0 +1,10 @@
|
||||
{% extends '@ChillMain/layout.html.twig' %}
|
||||
|
||||
{% block title %}
|
||||
{% include('@ChillMain/CRUD/_new_title.html.twig') %}
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
{% embed '@ChillMain/CRUD/_new_content.html.twig' %}
|
||||
{% endembed %}
|
||||
{% endblock %}
|
Loading…
x
Reference in New Issue
Block a user