mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge pull request #9631 from open-sausages/pulls/4/custom-sort-gridfield-autocompleter
This commit is contained in:
commit
766df06f23
@ -223,8 +223,8 @@ class GridFieldAddExistingAutocompleter implements GridField_HTMLProvider, GridF
|
|||||||
*/
|
*/
|
||||||
public function doSearch($gridField, $request)
|
public function doSearch($gridField, $request)
|
||||||
{
|
{
|
||||||
|
$searchStr = $request->getVar('gridfield_relationsearch');
|
||||||
$dataClass = $gridField->getModelClass();
|
$dataClass = $gridField->getModelClass();
|
||||||
$allList = $this->searchList ? $this->searchList : DataList::create($dataClass);
|
|
||||||
|
|
||||||
$searchFields = ($this->getSearchFields())
|
$searchFields = ($this->getSearchFields())
|
||||||
? $this->getSearchFields()
|
? $this->getSearchFields()
|
||||||
@ -241,12 +241,22 @@ class GridFieldAddExistingAutocompleter implements GridField_HTMLProvider, GridF
|
|||||||
$params = [];
|
$params = [];
|
||||||
foreach ($searchFields as $searchField) {
|
foreach ($searchFields as $searchField) {
|
||||||
$name = (strpos($searchField, ':') !== false) ? $searchField : "$searchField:StartsWith";
|
$name = (strpos($searchField, ':') !== false) ? $searchField : "$searchField:StartsWith";
|
||||||
$params[$name] = $request->getVar('gridfield_relationsearch');
|
$params[$name] = $searchStr;
|
||||||
}
|
}
|
||||||
$results = $allList
|
|
||||||
|
$results = null;
|
||||||
|
if ($this->searchList) {
|
||||||
|
// Assume custom sorting, don't apply default sorting
|
||||||
|
$results = $this->searchList;
|
||||||
|
} else {
|
||||||
|
$results = DataList::create($dataClass)
|
||||||
|
->sort(strtok($searchFields[0], ':'), 'ASC');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply baseline filtering and limits which should hold regardless of any customisations
|
||||||
|
$results = $results
|
||||||
->subtract($gridField->getList())
|
->subtract($gridField->getList())
|
||||||
->filterAny($params)
|
->filterAny($params)
|
||||||
->sort(strtok($searchFields[0], ':'), 'ASC')
|
|
||||||
->limit($this->getResultsLimit());
|
->limit($this->getResultsLimit());
|
||||||
|
|
||||||
$json = [];
|
$json = [];
|
||||||
|
@ -2,10 +2,14 @@
|
|||||||
|
|
||||||
namespace SilverStripe\Forms\Tests\GridField;
|
namespace SilverStripe\Forms\Tests\GridField;
|
||||||
|
|
||||||
|
use SilverStripe\Control\HTTPRequest;
|
||||||
use SilverStripe\Core\Convert;
|
use SilverStripe\Core\Convert;
|
||||||
use SilverStripe\Dev\CSSContentParser;
|
use SilverStripe\Dev\CSSContentParser;
|
||||||
use SilverStripe\Dev\FunctionalTest;
|
use SilverStripe\Dev\FunctionalTest;
|
||||||
|
use SilverStripe\Forms\GridField\GridField;
|
||||||
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
|
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
|
||||||
|
use SilverStripe\Forms\GridField\GridFieldConfig;
|
||||||
|
use SilverStripe\Forms\GridField\GridFieldDataColumns;
|
||||||
use SilverStripe\Forms\Tests\GridField\GridFieldAddExistingAutocompleterTest\TestController;
|
use SilverStripe\Forms\Tests\GridField\GridFieldAddExistingAutocompleterTest\TestController;
|
||||||
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Cheerleader;
|
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Cheerleader;
|
||||||
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Permissions;
|
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Permissions;
|
||||||
@ -127,4 +131,54 @@ class GridFieldAddExistingAutocompleterTest extends FunctionalTest
|
|||||||
new ArrayList([$team1, $team2])
|
new ArrayList([$team1, $team2])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testRetainsCustomSort()
|
||||||
|
{
|
||||||
|
$component = new GridFieldAddExistingAutocompleter($targetFragment = 'before', ['Test']);
|
||||||
|
$component->setSearchFields(['Name']);
|
||||||
|
|
||||||
|
$grid = $this->getGridFieldForComponent($component);
|
||||||
|
$grid->setList(Team::get()->filter('Name', 'force-empty-list'));
|
||||||
|
|
||||||
|
$component->setSearchList(Team::get());
|
||||||
|
$request = new HTTPRequest('GET', '', ['gridfield_relationsearch' => 'Team']);
|
||||||
|
$response = $component->doSearch($grid, $request);
|
||||||
|
$this->assertFalse($response->isError());
|
||||||
|
$result = json_decode($response->getBody(), true);
|
||||||
|
$this->assertEquals(
|
||||||
|
['Team 1', 'Team 2', 'Team 3', 'Team 4'],
|
||||||
|
array_map(
|
||||||
|
function ($item) {
|
||||||
|
return $item['label'];
|
||||||
|
},
|
||||||
|
$result
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$component->setSearchList(Team::get()->sort('Name', 'DESC'));
|
||||||
|
$request = new HTTPRequest('GET', '', ['gridfield_relationsearch' => 'Team']);
|
||||||
|
$response = $component->doSearch($grid, $request);
|
||||||
|
$this->assertFalse($response->isError());
|
||||||
|
$result = json_decode($response->getBody(), true);
|
||||||
|
$this->assertEquals(
|
||||||
|
['Team 4', 'Team 3', 'Team 2', 'Team 1'],
|
||||||
|
array_map(
|
||||||
|
function ($item) {
|
||||||
|
return $item['label'];
|
||||||
|
},
|
||||||
|
$result
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getGridFieldForComponent($component)
|
||||||
|
{
|
||||||
|
$config = GridFieldConfig::create()->addComponents(
|
||||||
|
$component,
|
||||||
|
new GridFieldDataColumns()
|
||||||
|
);
|
||||||
|
|
||||||
|
return (new GridField('testfield', 'testfield'))
|
||||||
|
->setConfig($config);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user