mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge pull request #10733 from creative-commoners/pulls/4.13/searchable
FIX Respect searchable_fields
This commit is contained in:
commit
a62e554409
@ -254,21 +254,25 @@ class GridFieldFilterHeader extends AbstractGridFieldComponent implements GridFi
|
|||||||
public function canFilterAnyColumns($gridField)
|
public function canFilterAnyColumns($gridField)
|
||||||
{
|
{
|
||||||
$list = $gridField->getList();
|
$list = $gridField->getList();
|
||||||
|
if (!($list instanceof Filterable) || !$this->checkDataType($list)) {
|
||||||
if (!$this->checkDataType($list)) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
$modelClass = $gridField->getModelClass();
|
||||||
$columns = $gridField->getColumns();
|
// note: searchableFields() will return summary_fields if there are no searchable_fields on the model
|
||||||
foreach ($columns as $columnField) {
|
$searchableFields = array_keys($modelClass::singleton()->searchableFields());
|
||||||
$metadata = $gridField->getColumnMetadata($columnField);
|
$summaryFields = array_keys($modelClass::singleton()->summaryFields());
|
||||||
$title = $metadata['title'];
|
sort($searchableFields);
|
||||||
|
sort($summaryFields);
|
||||||
if ($title && $list->canFilterBy($columnField)) {
|
// searchable_fields has been explictily defined i.e. searchableFields() is not falling back to summary_fields
|
||||||
|
if ($searchableFields !== $summaryFields) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// we have fallen back to summary_fields, check they are filterable
|
||||||
|
foreach ($searchableFields as $searchableField) {
|
||||||
|
if ($list->canFilterBy($searchableField)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -182,4 +182,30 @@ class GridFieldFilterHeaderTest extends SapphireTest
|
|||||||
|
|
||||||
$this->assertEquals('ReallyCustomSearch', $this->component->getSearchField());
|
$this->assertEquals('ReallyCustomSearch', $this->component->getSearchField());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testCanFilterAnyColumns()
|
||||||
|
{
|
||||||
|
$gridField = $this->gridField;
|
||||||
|
$filterHeader = $gridField->getConfig()->getComponentByType(GridFieldFilterHeader::class);
|
||||||
|
|
||||||
|
// test that you can filter by something if searchable_fields is not defined
|
||||||
|
// silverstripe will scaffold db columns that are in the gridfield to be
|
||||||
|
// searchable by default
|
||||||
|
Config::modify()->remove(Team::class, 'searchable_fields');
|
||||||
|
$this->assertTrue($filterHeader->canFilterAnyColumns($gridField));
|
||||||
|
|
||||||
|
// test that you can filterBy if searchable_fields is defined
|
||||||
|
Config::modify()->set(Team::class, 'searchable_fields', ['Name']);
|
||||||
|
$this->assertTrue($filterHeader->canFilterAnyColumns($gridField));
|
||||||
|
|
||||||
|
// test that you can filterBy if searchable_fields even if it is not a legit field
|
||||||
|
// this is because we're making a blind assumption it will be filterable later in a SearchContext
|
||||||
|
Config::modify()->set(Team::class, 'searchable_fields', ['WhatIsThis']);
|
||||||
|
$this->assertTrue($filterHeader->canFilterAnyColumns($gridField));
|
||||||
|
|
||||||
|
// test that you cannot filter by non-db field when it falls back to summary_fields
|
||||||
|
Config::modify()->remove(Team::class, 'searchable_fields');
|
||||||
|
Config::modify()->set(Team::class, 'summary_fields', ['MySummaryField']);
|
||||||
|
$this->assertFalse($filterHeader->canFilterAnyColumns($gridField));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,4 +24,9 @@ class Team extends DataObject implements TestOnly
|
|||||||
'Cheerleader' => Cheerleader::class,
|
'Cheerleader' => Cheerleader::class,
|
||||||
'CheerleadersMom' => Mom::class
|
'CheerleadersMom' => Mom::class
|
||||||
];
|
];
|
||||||
|
|
||||||
|
public function getMySummaryField()
|
||||||
|
{
|
||||||
|
return 'MY SUMMARY FIELD';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user