mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Fix ArrayList canFilterBy to work with ArrayData (#10915)
This commit is contained in:
parent
cdbc50cb7e
commit
c7cd26299a
@ -604,7 +604,15 @@ class ArrayList extends ViewableData implements SS_List, Filterable, Sortable, L
|
||||
|
||||
$firstRecord = $this->first();
|
||||
|
||||
return is_array($firstRecord) ? array_key_exists($by, $firstRecord) : property_exists($firstRecord, $by ?? '');
|
||||
if (is_array($firstRecord)) {
|
||||
return array_key_exists($by, $firstRecord);
|
||||
}
|
||||
|
||||
if ($firstRecord instanceof ViewableData) {
|
||||
return $firstRecord->hasField($by);
|
||||
}
|
||||
|
||||
return property_exists($firstRecord, $by ?? '');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -7,6 +7,7 @@ use SilverStripe\Dev\SapphireTest;
|
||||
use SilverStripe\ORM\ArrayList;
|
||||
use SilverStripe\ORM\DataObject;
|
||||
use SilverStripe\ORM\Filterable;
|
||||
use SilverStripe\View\ArrayData;
|
||||
use stdClass;
|
||||
|
||||
class ArrayListTest extends SapphireTest
|
||||
@ -1174,6 +1175,20 @@ class ArrayListTest extends SapphireTest
|
||||
$this->assertFalse($list->canFilterBy('Age'));
|
||||
}
|
||||
|
||||
public function testCanFilterByArrayData()
|
||||
{
|
||||
$list = new ArrayList(
|
||||
[
|
||||
new ArrayData(['Name' => 'Steve']),
|
||||
new ArrayData(['Name' => 'Bob']),
|
||||
new ArrayData(['Name' => 'John'])
|
||||
]
|
||||
);
|
||||
|
||||
$this->assertTrue($list->canFilterBy('Name'));
|
||||
$this->assertFalse($list->canFilterBy('Age'));
|
||||
}
|
||||
|
||||
public function testCanFilterByEmpty()
|
||||
{
|
||||
$list = new ArrayList();
|
||||
|
Loading…
Reference in New Issue
Block a user