Skip to content

Commit

Permalink
bug #1872 Fixed the custom sorting of the "search" view (javiereguiluz)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 1.16.x-dev branch (closes #1872).

Discussion
----------

Fixed the custom sorting of the "search" view

This fixes #1570.

Commits
-------

e4a1124 Fixed the custom sorting of the "search" view
  • Loading branch information
javiereguiluz committed Oct 20, 2017
2 parents 1fda745 + e4a1124 commit cf48848
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
11 changes: 10 additions & 1 deletion src/Controller/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,16 @@ protected function searchAction()
}

$searchableFields = $this->entity['search']['fields'];
$paginator = $this->findBy($this->entity['class'], $this->request->query->get('query'), $searchableFields, $this->request->query->get('page', 1), $this->config['list']['max_results'], $this->request->query->get('sortField'), $this->request->query->get('sortDirection'), $this->entity['search']['dql_filter']);
$paginator = $this->findBy(
$this->entity['class'],
$this->request->query->get('query'),
$searchableFields,
$this->request->query->get('page', 1),
$this->config['list']['max_results'],
isset($this->entity['search']['sort']['field']) ? $this->entity['search']['sort']['field'] : $this->request->query->get('sortField'),
isset($this->entity['search']['sort']['direction']) ? $this->entity['search']['sort']['direction'] : $this->request->query->get('sortDirection'),
$this->entity['search']['dql_filter']
);
$fields = $this->entity['list']['fields'];

$this->dispatch(EasyAdminEvents::POST_SEARCH, array(
Expand Down
10 changes: 7 additions & 3 deletions src/Resources/views/default/list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
}) %}

{% if 'search' == app.request.get('action') %}
{% set _request_parameters = _request_parameters|merge({ query: app.request.get('query')|default('') }) %}
{% set _request_parameters = _request_parameters|merge({
query: app.request.get('query')|default(''),
sortField: _entity_config.search.sort.field|default(app.request.get('sortField', '')),
sortDirection: _entity_config.search.sort.direction|default(app.request.get('sortDirection', 'DESC')),
}) %}
{% endif %}

{% set _request_parameters = _request_parameters|merge({ referer: path('easyadmin', _request_parameters)|url_encode }) %}
Expand Down Expand Up @@ -55,8 +59,8 @@
{% block search_form %}
<input type="hidden" name="action" value="search">
<input type="hidden" name="entity" value="{{ _request_parameters.entity }}">
<input type="hidden" name="sortField" value="{{ _request_parameters.sortField }}">
<input type="hidden" name="sortDirection" value="{{ _request_parameters.sortDirection }}">
<input type="hidden" name="sortField" value="{{ _entity_config.search.sort.field|default(_request_parameters.sortField) }}">
<input type="hidden" name="sortDirection" value="{{ _entity_config.search.sort.direction|default(_request_parameters.sortDirection) }}">
<input type="hidden" name="menuIndex" value="{{ _request_parameters.menuIndex }}">
<input type="hidden" name="submenuIndex" value="{{ _request_parameters.submenuIndex }}">
<div class="input-group">
Expand Down
12 changes: 6 additions & 6 deletions tests/Controller/CustomizedBackendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public function testListViewSearchAction()
$hiddenParameters = array(
'action' => 'search',
'entity' => 'Category',
'sortField' => 'id',
'sortDirection' => 'DESC',
'sortField' => 'name',
'sortDirection' => 'ASC',
);

$this->assertSame('Look for Categories', trim($crawler->filter('.action-search button[type=submit]')->text()));
Expand Down Expand Up @@ -564,8 +564,8 @@ public function testSearchViewDefaultTableSorting()
$crawler = $this->requestSearchView();

$this->assertCount(1, $crawler->filter('.table thead th[class*="sorted"]'), 'Table is sorted only by one column.');
$this->assertSame('ID', trim($crawler->filter('.table thead th[class*="sorted"]')->text()), 'By default, table is soreted by ID column.');
$this->assertSame('fa fa-caret-down', $crawler->filter('.table thead th[class*="sorted"] i')->attr('class'), 'The column used to sort results shows the right icon.');
$this->assertSame('Label', trim($crawler->filter('.table thead th[class*="sorted"]')->text()), 'By default, table is soreted by "Label" column (which is the "name" property).');
$this->assertSame('fa fa-caret-up', $crawler->filter('.table thead th[class*="sorted"] i')->attr('class'), 'The column used to sort results shows the right icon.');
}

public function testSearchViewTableContents()
Expand Down Expand Up @@ -594,8 +594,8 @@ public function testSearchViewPagination()
$this->assertSame('disabled', $crawler->filter('.list-pagination li:contains("First")')->attr('class'));
$this->assertSame('disabled', $crawler->filter('.list-pagination li:contains("Previous")')->attr('class'));

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

public function testSearchViewItemActions()
Expand Down
3 changes: 3 additions & 0 deletions tests/Fixtures/App/config/config_customized_backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ easy_admin:
label: 'Categories'
list:
title: 'Product %%entity_label%%'
sort: ['id', 'DESC']
actions:
- 'show'
- { name: 'new', label: 'New %%entity_name%%', icon: 'plus-circle', css_class: 'custom_class_new' }
Expand All @@ -94,6 +95,8 @@ easy_admin:
- 'id'
- { property: 'name', label: 'Label', css_class: 'custom_class_list' }
- { property: 'parent', label: 'Parent category' }
search:
sort: ['name', 'ASC']
show:
title: 'Details for %%entity_name%% number %%entity_id%%'
help: 'Help message overridden for the show view of categories'
Expand Down

0 comments on commit cf48848

Please sign in to comment.