mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
NEW: Allow search field customisation in GridFieldFilterHeader
This commit is contained in:
parent
b62c4a9f53
commit
07a6c1191a
@ -76,6 +76,12 @@ class GridFieldFilterHeader extends AbstractGridFieldComponent implements GridFi
|
|||||||
*/
|
*/
|
||||||
protected $updateSearchFormCallback = null;
|
protected $updateSearchFormCallback = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The name of the default search field
|
||||||
|
* @var string|null
|
||||||
|
*/
|
||||||
|
protected ?string $searchField = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
*/
|
*/
|
||||||
@ -125,6 +131,17 @@ class GridFieldFilterHeader extends AbstractGridFieldComponent implements GridFi
|
|||||||
return $this->throwExceptionOnBadDataType;
|
return $this->throwExceptionOnBadDataType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getSearchField(): ?string
|
||||||
|
{
|
||||||
|
return $this->searchField;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setSearchField(string $field): self
|
||||||
|
{
|
||||||
|
$this->searchField = $field;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check that this dataList is of the right data type.
|
* Check that this dataList is of the right data type.
|
||||||
* Returns false if it's a bad data type, and if appropriate, throws an exception.
|
* Returns false if it's a bad data type, and if appropriate, throws an exception.
|
||||||
@ -281,7 +298,7 @@ class GridFieldFilterHeader extends AbstractGridFieldComponent implements GridFi
|
|||||||
public function getSearchFieldSchema(GridField $gridField)
|
public function getSearchFieldSchema(GridField $gridField)
|
||||||
{
|
{
|
||||||
$schemaUrl = Controller::join_links($gridField->Link(), 'schema/SearchForm');
|
$schemaUrl = Controller::join_links($gridField->Link(), 'schema/SearchForm');
|
||||||
|
$inst = singleton($gridField->getModelClass());
|
||||||
$context = $this->getSearchContext($gridField);
|
$context = $this->getSearchContext($gridField);
|
||||||
$params = $gridField->getRequest()->postVar('filter') ?: [];
|
$params = $gridField->getRequest()->postVar('filter') ?: [];
|
||||||
if (array_key_exists($gridField->getName(), $params ?? [])) {
|
if (array_key_exists($gridField->getName(), $params ?? [])) {
|
||||||
@ -292,10 +309,13 @@ class GridFieldFilterHeader extends AbstractGridFieldComponent implements GridFi
|
|||||||
}
|
}
|
||||||
$context->setSearchParams($params);
|
$context->setSearchParams($params);
|
||||||
|
|
||||||
$searchField = $context->getSearchFields()->first();
|
$searchField = $this->getSearchField() ?: $inst->config()->get('general_search_field');
|
||||||
$searchField = $searchField && property_exists($searchField, 'name') ? $searchField->name : null;
|
if (!$searchField) {
|
||||||
|
$searchField = $context->getSearchFields()->first();
|
||||||
|
$searchField = $searchField && property_exists($searchField, 'name') ? $searchField->name : null;
|
||||||
|
}
|
||||||
|
|
||||||
$name = $gridField->Title ?: singleton($gridField->getModelClass())->i18n_plural_name();
|
$name = $gridField->Title ?: $inst->i18n_plural_name();
|
||||||
|
|
||||||
// Prefix "Search__" onto the filters for the React component
|
// Prefix "Search__" onto the filters for the React component
|
||||||
$filters = $context->getSearchParams();
|
$filters = $context->getSearchParams();
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
namespace SilverStripe\Forms\Tests\GridField;
|
namespace SilverStripe\Forms\Tests\GridField;
|
||||||
|
|
||||||
use SilverStripe\Control\HTTPRequest;
|
use SilverStripe\Control\HTTPRequest;
|
||||||
|
use SilverStripe\Core\Config\Config;
|
||||||
use SilverStripe\Dev\SapphireTest;
|
use SilverStripe\Dev\SapphireTest;
|
||||||
use SilverStripe\Forms\FieldList;
|
use SilverStripe\Forms\FieldList;
|
||||||
use SilverStripe\Forms\Form;
|
use SilverStripe\Forms\Form;
|
||||||
@ -156,4 +157,20 @@ class GridFieldFilterHeaderTest extends SapphireTest
|
|||||||
$this->assertEquals('no-change-track', $field->extraClasses['no-change-track']);
|
$this->assertEquals('no-change-track', $field->extraClasses['no-change-track']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testCustomSearchField()
|
||||||
|
{
|
||||||
|
$searchSchema = json_decode($this->component->getSearchFieldSchema($this->gridField));
|
||||||
|
$this->assertEquals('Name', $searchSchema->name);
|
||||||
|
|
||||||
|
Config::modify()->set(Team::class, 'general_search_field', 'CustomSearch');
|
||||||
|
$searchSchema = json_decode($this->component->getSearchFieldSchema($this->gridField));
|
||||||
|
$this->assertEquals('CustomSearch', $searchSchema->name);
|
||||||
|
|
||||||
|
$this->component->setSearchField('ReallyCustomSearch');
|
||||||
|
$searchSchema = json_decode($this->component->getSearchFieldSchema($this->gridField));
|
||||||
|
$this->assertEquals('ReallyCustomSearch', $searchSchema->name);
|
||||||
|
|
||||||
|
$this->assertEquals('ReallyCustomSearch', $this->component->getSearchField());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user