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();
|
$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\ArrayList;
|
||||||
use SilverStripe\ORM\DataObject;
|
use SilverStripe\ORM\DataObject;
|
||||||
use SilverStripe\ORM\Filterable;
|
use SilverStripe\ORM\Filterable;
|
||||||
|
use SilverStripe\View\ArrayData;
|
||||||
use stdClass;
|
use stdClass;
|
||||||
|
|
||||||
class ArrayListTest extends SapphireTest
|
class ArrayListTest extends SapphireTest
|
||||||
|
@ -1174,6 +1175,20 @@ class ArrayListTest extends SapphireTest
|
||||||
$this->assertFalse($list->canFilterBy('Age'));
|
$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()
|
public function testCanFilterByEmpty()
|
||||||
{
|
{
|
||||||
$list = new ArrayList();
|
$list = new ArrayList();
|
||||||
|
|
Loading…
Reference in New Issue