Skip to content

Commit bd783a4

Browse files
Merge pull request #361 from creative-commoners/pulls/3.5/arrays
FIX Handle arrays in default_sort
2 parents e9f202f + 17f92d0 commit bd783a4

3 files changed

Lines changed: 42 additions & 0 deletions

File tree

src/GridFieldOrderableRows.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,13 @@ public function getManipulatedData(GridField $grid, SS_List $list)
365365
if ($list instanceof DataList) {
366366
$classname = $list->dataClass();
367367
if ($defaultSort = Config::inst()->get($classname, 'default_sort')) {
368+
if (is_array($defaultSort)) {
369+
$defaultSortArray = [];
370+
foreach ($defaultSort as $column => $direction) {
371+
$defaultSortArray[] = "\"$column\" $direction";
372+
}
373+
$defaultSort = implode(', ', $defaultSortArray);
374+
}
368375
// Append the default sort to the end of the sort string
369376
// This may result in redundancy... but it seems to work
370377
$sortterm .= ($sortterm ? ', ' : '') . $defaultSort;

tests/GridFieldOrderableRowsTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Symbiote\GridFieldExtensions\Tests\Stub\ThroughIntermediary;
2525
use Symbiote\GridFieldExtensions\Tests\Stub\TitleObject;
2626
use Symbiote\GridFieldExtensions\Tests\Stub\TitleSortedObject;
27+
use Symbiote\GridFieldExtensions\Tests\Stub\TitleArraySortedObject;
2728
use Symbiote\GridFieldExtensions\Tests\Stub\ThroughIntermediaryVersioned;
2829

2930
/**
@@ -53,6 +54,7 @@ class GridFieldOrderableRowsTest extends SapphireTest
5354
ThroughBelongs::class,
5455
TitleObject::class,
5556
TitleSortedObject::class,
57+
TitleArraySortedObject::class,
5658
ThroughDefinerVersioned::class,
5759
ThroughIntermediaryVersioned::class,
5860
ThroughBelongsVersioned::class,
@@ -332,6 +334,17 @@ public function testGetManipulatedDataWithDefaultSort()
332334
$this->assertSame(['B', 'A', 'C'], $sortedList->column('Iden'));
333335
}
334336

337+
338+
public function testGetManipulatedDataWithDefaultSortArray()
339+
{
340+
$sortedList = $this->getTitleSortedListForManipuatedData(TitleArraySortedObject::class, [
341+
['Title' => 'X', 'Iden' => 'C', 'OtherSort' => 3],
342+
['Title' => 'Z', 'Iden' => 'A', 'OtherSort' => 2],
343+
['Title' => 'Z', 'Iden' => 'B', 'OtherSort' => 1],
344+
]);
345+
$this->assertSame(['C', 'B', 'A'], $sortedList->column('Iden'));
346+
}
347+
335348
private function getTitleSortedListForManipuatedData(string $dataClass, array $data): DataList
336349
{
337350
$list = new DataList($dataClass);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Symbiote\GridFieldExtensions\Tests\Stub;
4+
5+
use SilverStripe\Dev\TestOnly;
6+
use SilverStripe\ORM\DataObject;
7+
8+
class TitleArraySortedObject extends DataObject implements TestOnly
9+
{
10+
private static $db = [
11+
'Title' => 'Varchar',
12+
'Iden' => 'Varchar',
13+
'OtherSort' => 'Int'
14+
];
15+
16+
private static array $default_sort = [
17+
'Title' => 'ASC',
18+
'OtherSort' => 'ASC',
19+
];
20+
21+
private static $table_name = 'TitleArraySortedObject';
22+
}

0 commit comments

Comments
 (0)