Fix ArrayList canFilterBy to work with ArrayData (#10915)

This commit is contained in:
Guy Sartorelli 2023-08-15 11:31:20 +12:00 committed by GitHub
parent cdbc50cb7e
commit c7cd26299a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 1 deletions

View File

@ -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 ?? '');
}
/**

View File

@ -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();