Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX Respect searchable_fields #10733

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/Forms/GridField/GridFieldFilterHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,21 +254,25 @@ public function getManipulatedData(GridField $gridField, SS_List $dataList)
public function canFilterAnyColumns($gridField)
{
$list = $gridField->getList();

if (!$this->checkDataType($list)) {
if (!($list instanceof Filterable) || !$this->checkDataType($list)) {
return false;
}

$columns = $gridField->getColumns();
foreach ($columns as $columnField) {
$metadata = $gridField->getColumnMetadata($columnField);
$title = $metadata['title'];

if ($title && $list->canFilterBy($columnField)) {
$modelClass = $gridField->getModelClass();
// note: searchableFields() will return summary_fields if there are no searchable_fields on the model
$searchableFields = array_keys($modelClass::singleton()->searchableFields());
$summaryFields = array_keys($modelClass::singleton()->summaryFields());
sort($searchableFields);
sort($summaryFields);
// 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 false;
}

Expand Down
26 changes: 26 additions & 0 deletions tests/php/Forms/GridField/GridFieldFilterHeaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,30 @@ public function testCustomSearchField()

$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));
}
}
5 changes: 5 additions & 0 deletions tests/php/Forms/GridField/GridFieldFilterHeaderTest/Team.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ class Team extends DataObject implements TestOnly
'Cheerleader' => Cheerleader::class,
'CheerleadersMom' => Mom::class
];

public function getMySummaryField()
{
return 'MY SUMMARY FIELD';
}
}