From d2dbe7460213c1e963fa1a4cbcd042fdcc645f7f Mon Sep 17 00:00:00 2001 From: Aaron Carlino Date: Wed, 27 Nov 2019 11:38:38 +1300 Subject: [PATCH] NEW: Allow search field customisation in GridFieldFilterHeader --- src/Forms/GridField/GridFieldFilterHeader.php | 29 ++++++++++++++++--- .../GridField/GridFieldFilterHeaderTest.php | 17 +++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/Forms/GridField/GridFieldFilterHeader.php b/src/Forms/GridField/GridFieldFilterHeader.php index 43e781a9cd3..bfa64e017ab 100755 --- a/src/Forms/GridField/GridFieldFilterHeader.php +++ b/src/Forms/GridField/GridFieldFilterHeader.php @@ -76,6 +76,12 @@ class GridFieldFilterHeader extends AbstractGridFieldComponent implements GridFi */ protected $updateSearchFormCallback = null; + /** + * The name of the default search field + * @var string|null + */ + protected ?string $searchField = null; + /** * @inheritDoc */ @@ -125,6 +131,18 @@ public function getThrowExceptionOnBadDataType() 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. * Returns false if it's a bad data type, and if appropriate, throws an exception. @@ -281,7 +299,7 @@ public function getSearchContext(GridField $gridField) public function getSearchFieldSchema(GridField $gridField) { $schemaUrl = Controller::join_links($gridField->Link(), 'schema/SearchForm'); - + $inst = singleton($gridField->getModelClass()); $context = $this->getSearchContext($gridField); $params = $gridField->getRequest()->postVar('filter') ?: []; if (array_key_exists($gridField->getName(), $params ?? [])) { @@ -292,10 +310,13 @@ public function getSearchFieldSchema(GridField $gridField) } $context->setSearchParams($params); - $searchField = $context->getSearchFields()->first(); - $searchField = $searchField && property_exists($searchField, 'name') ? $searchField->name : null; + $searchField = $this->getSearchField() ?: $inst->config()->get('primary_search_field'); + 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 $filters = $context->getSearchParams(); diff --git a/tests/php/Forms/GridField/GridFieldFilterHeaderTest.php b/tests/php/Forms/GridField/GridFieldFilterHeaderTest.php index e7bf6a3fbbb..d007f998117 100644 --- a/tests/php/Forms/GridField/GridFieldFilterHeaderTest.php +++ b/tests/php/Forms/GridField/GridFieldFilterHeaderTest.php @@ -3,6 +3,7 @@ namespace SilverStripe\Forms\Tests\GridField; use SilverStripe\Control\HTTPRequest; +use SilverStripe\Core\Config\Config; use SilverStripe\Dev\SapphireTest; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\Form; @@ -156,4 +157,20 @@ public function testGetSearchForm() $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, 'primary_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()); + } }