Skip to content

Commit

Permalink
feature #280 Return to the previous list/search page after editing/de…
Browse files Browse the repository at this point in the history
…leting/showing an entity (javiereguiluz)

This PR was squashed before being merged into the master branch (closes #280).

Discussion
----------

Return to the previous list/search page after editing/deleting/showing an entity

This fixes #272

Commits
-------

2708dbb Return to the previous list/search page after editing/deleting/showing an entity
  • Loading branch information
javiereguiluz committed May 7, 2015
2 parents 827d335 + 2708dbb commit 3e5ac07
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 16 deletions.
20 changes: 16 additions & 4 deletions Controller/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,11 @@ protected function editAction()
$this->prepareEditEntityForPersist($item);
$this->em->flush();

return $this->redirect($this->generateUrl('admin', array('action' => 'list', 'view' => 'list', 'entity' => $this->entity['name'])));
$refererUrl = $this->request->query->get('referer', '');

return !empty($refererUrl)
? $this->redirect(urldecode($refererUrl))
: $this->redirect($this->generateUrl('admin', array('action' => 'list', 'view' => 'list', 'entity' => $this->entity['name'])));
}

return $this->render($this->entity['templates']['edit'], array(
Expand Down Expand Up @@ -231,7 +235,11 @@ protected function newAction()
$this->em->persist($item);
$this->em->flush();

return $this->redirect($this->generateUrl('admin', array('action' => 'list', 'view' => 'new', 'entity' => $this->entity['name'])));
$refererUrl = $this->request->query->get('referer', '');

return !empty($refererUrl)
? $this->redirect(urldecode($refererUrl))
: $this->redirect($this->generateUrl('admin', array('action' => 'list', 'view' => 'new', 'entity' => $this->entity['name'])));
}

return $this->render($this->entity['templates']['new'], array(
Expand Down Expand Up @@ -260,14 +268,18 @@ protected function deleteAction()

if ($form->isValid()) {
if (!$entity = $this->em->getRepository($this->entity['class'])->find($id)) {
throw $this->createNotFoundException('The entity to be delete does not exist.');
throw $this->createNotFoundException('The entity to be deleted does not exist.');
}

$this->em->remove($entity);
$this->em->flush();
}

return $this->redirect($this->generateUrl('admin', array('action' => 'list', 'view' => 'list', 'entity' => $this->entity['name'])));
$refererUrl = $this->request->query->get('referer', '');

return !empty($refererUrl)
? $this->redirect(urldecode($refererUrl))
: $this->redirect($this->generateUrl('admin', array('action' => 'list', 'view' => 'list', 'entity' => $this->entity['name'])));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Resources/doc/10-customizing-design.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ and make it extend from the default `list` template. Then, override the

```twig
{# app/Resources/views/easy_admin/list.html.twig #}
{% extends @EasyAdmin/default/list.html.twig %}
{% extends '@EasyAdmin/default/list.html.twig' %}
{% block search_action %}
{# ... #}
Expand Down
8 changes: 7 additions & 1 deletion Resources/views/default/edit.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@
{% endblock entity_form %}

{% block delete_form %}
{{ form(delete_form, { attr: { id: 'delete-form', style: 'display: none' }}) }}
{{
form(delete_form, {
action: delete_form.vars.action ~ '&referer=' ~ app.request.query.get('referer', ''),
method: 'DELETE',
attr: { id: 'delete-form', style: 'display: none' }
})
}}

<div id="modal-delete" class="modal fade">
<div class="modal-dialog">
Expand Down
4 changes: 3 additions & 1 deletion Resources/views/default/form.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
{{ form_start(form, { attr: form.vars.attr|merge({ novalidate: 'novalidate' }) }) }}
<div id="form row">
{% for field in form.children if 'hidden' not in field.vars.block_prefixes %}
<input type="hidden" name="referer" value="{{ app.request.query.get('referer', '') }}" />

{% set _field_metadata = entity_fields[field.vars.name] %}

{% if _field_metadata['fieldType'] == 'association' %}
Expand Down Expand Up @@ -81,7 +83,7 @@
{% endif %}

{% if _list_action is defined %}
<a class="btn btn-secondary" href="{{ path('admin', ({ entity: _entity.name, action: _list_action.name, view: view }) ) }}">{% spaceless %}
<a class="btn btn-secondary" href="{{ app.request.query.has('referer') ? app.request.query.get('referer') : path('admin', ({ entity: _entity.name, action: _list_action.name, view: view }) ) }}">{% spaceless %}
{% if _list_action.icon %}<i class="fa fa-{{ _list_action.icon }}"></i>{% endif %}
{{ _list_action.label|default('action.list')|trans(_trans_parameters) }}
{% endspaceless %}</a>
Expand Down
8 changes: 5 additions & 3 deletions Resources/views/default/list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
{% endblock %}

{% block content %}
{% set _request_parameters = { view: 'list', action: app.request.get('action'), entity: _entity.name, sortField: app.request.get('sortField', ''), sortDirection: app.request.get('sortDirection', 'DESC') } %}
{% set _request_parameters = { view: 'list', action: app.request.get('action'), entity: _entity.name, sortField: app.request.get('sortField', ''), sortDirection: app.request.get('sortDirection', 'DESC'), page: app.request.get('page', 1) } %}

{% if 'search' == app.request.get('action') %}
{% set _request_parameters = _request_parameters|merge({ query: app.request.get('query')|default('') }) %}
{% endif %}

{% set _request_parameters = _request_parameters|merge({ referer: path('admin', _request_parameters)|url_encode }) %}

<div class="row">
<div id="content-header" class="col-sm-12">
{% block content_header %}
Expand Down Expand Up @@ -136,9 +138,9 @@
{% block item_actions %}
{% for _action in _list_item_actions %}
{% if 'method' == _action.type %}
{% set _action_href = path('admin', { action: _action.name, view: 'list', entity: _entity.name, id: _item_id }) %}
{% set _action_href = path('admin', _request_parameters|merge({ action: _action.name, id: _item_id })) %}
{% elseif 'route' == _action.type %}
{% set _action_href = path(_action.name, { entity: _entity.name, id: _item_id }) %}
{% set _action_href = path(_action.name, _request_parameters|merge({ action: _action.name, id: _item_id })) %}
{% endif %}

<a class="{{ _action.class|default('') }}" href="{{ _action_href }}">{% spaceless %}
Expand Down
14 changes: 10 additions & 4 deletions Resources/views/default/show.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
{% set _show_actions = easyadmin_get_actions_for_show_item(_entity.name) %}
{% for _action in _show_actions %}
{% if 'method' == _action.type %}
{% set _action_href = path('admin', { action: _action.name, view: 'show', entity: _entity.name, id: attribute(item, _entity.primary_key_field_name) }) %}
{% set _action_href = path('admin', { action: _action.name, view: 'show', entity: _entity.name, id: attribute(item, _entity.primary_key_field_name), referer: app.request.query.get('referer', '') }) %}
{% elseif 'route' == _action.type %}
{% set _action_href = path(_action.name, { entity: _entity.name, id: attribute(item, _entity.primary_key_field_name) }) %}
{% set _action_href = path(_action.name, { entity: _entity.name, id: attribute(item, _entity.primary_key_field_name), referer: app.request.query.get('referer', '') }) %}
{% endif %}

<a class="btn {{ _action.class|default('') }}" href="{{ _action_href }}">
Expand All @@ -62,7 +62,7 @@
{# for aesthetic reasons, the 'list' action is always displayed as a link instead of a button #}
{% if easyadmin_action_is_enabled_for_show_view('list', _entity.name) %}
{% set _action = easyadmin_get_action_for_show_view('list', _entity.name) %}
<a class="btn btn-list btn-secondary" href="{{ path('admin', ({ entity: _entity.name, action: _action.name, view: 'show' }) ) }}">{% spaceless %}
<a class="btn btn-list btn-secondary" href="{{ app.request.query.has('referer') ? app.request.query.get('referer') : path('admin', ({ entity: _entity.name, action: _action.name, view: 'show' })) }}">{% spaceless %}
{% if _action.icon %}<i class="fa fa-{{ _action.icon }}"></i>{% endif %}
{{ _action.label|default('action.list')|trans(_trans_parameters) }}
{% endspaceless %}</a>
Expand All @@ -73,7 +73,13 @@
</div>

{% block delete_form %}
{{ form(delete_form, { attr: { id: 'delete-form', style: 'display: none' }}) }}
{{
form(delete_form, {
action: delete_form.vars.action ~ '&referer=' ~ app.request.query.get('referer', ''),
method: 'DELETE',
attr: { id: 'delete-form', style: 'display: none' }
})
}}

<div id="modal-delete" class="modal fade">
<div class="modal-dialog">
Expand Down
4 changes: 2 additions & 2 deletions Tests/AppBundleTest/CategoryEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ public function testListViewPagination()
$this->assertEquals('disabled', $crawler->filter('.list-pagination li:contains("First")')->attr('class'));
$this->assertEquals('disabled', $crawler->filter('.list-pagination li:contains("Previous")')->attr('class'));

$this->assertEquals('/admin/?view=list&action=list&entity=Category&sortField=id&sortDirection=DESC&page=2', $crawler->filter('.list-pagination li a:contains("Next")')->attr('href'));
$this->assertEquals('/admin/?view=list&action=list&entity=Category&sortField=id&sortDirection=DESC&page=14', $crawler->filter('.list-pagination li a:contains("Last")')->attr('href'));
$this->assertStringStartsWith('/admin/?view=list&action=list&entity=Category&sortField=id&sortDirection=DESC&page=2', $crawler->filter('.list-pagination li a:contains("Next")')->attr('href'));
$this->assertStringStartsWith('/admin/?view=list&action=list&entity=Category&sortField=id&sortDirection=DESC&page=14', $crawler->filter('.list-pagination li a:contains("Last")')->attr('href'));
}

public function testShowViewPageTitle()
Expand Down

0 comments on commit 3e5ac07

Please sign in to comment.