From 1159c29484674639618768921ffad4f6187142b0 Mon Sep 17 00:00:00 2001 From: Mojmir Fendek Date: Wed, 22 Jan 2025 12:54:49 +1300 Subject: [PATCH] ENH: Localisation manager filter. --- src/Extension/Traits/FluentObjectTrait.php | 37 ++++++++++++++++++- .../Extension/FluentFilteredExtensionTest.php | 5 ++- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/Extension/Traits/FluentObjectTrait.php b/src/Extension/Traits/FluentObjectTrait.php index 3a626e1d..c39f2b33 100644 --- a/src/Extension/Traits/FluentObjectTrait.php +++ b/src/Extension/Traits/FluentObjectTrait.php @@ -16,7 +16,9 @@ use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataQuery; use SilverStripe\ORM\FieldType\DBField; +use SilverStripe\ORM\Filters\PartialMatchFilter; use SilverStripe\ORM\Queries\SQLSelect; +use SilverStripe\ORM\Search\BasicSearchContext; use TractorCow\Fluent\Model\Locale; use TractorCow\Fluent\State\FluentState; @@ -107,9 +109,8 @@ protected function updateFluentCMSFields(FieldList $fields) // Generate gridfield for handling localisations $config = GridFieldConfig_Base::create(); - // Remove filters as the displayed data is in ArrayList format - $config->removeComponentsByType(GridFieldFilterHeader::class); + /** @var GridFieldDataColumns $columns */ $columns = $config->getComponentByType(GridFieldDataColumns::class); $summaryColumns = [ 'Title' => 'Title', @@ -163,6 +164,38 @@ protected function updateFluentCMSFields(FieldList $fields) $this->LinkedLocales(), $config ); + + /** @var GridFieldFilterHeader $filterHeader */ + $filterHeader = $config->getComponentByType(GridFieldFilterHeader::class); + + // Replace scaffolded filters as the displayed data is in ArrayList format so scaffolded filters do not work + if ($filterHeader) { + // Retrieve filters settings as these can be carried over as is + $defaultSearchContext = $filterHeader->getSearchContext($gridField); + $defaultSearchFields = $defaultSearchContext->getSearchFields(); + $defaultFilters = $defaultSearchContext->getFilters(); + + /** @var ArrayList $list */ + $list = $gridField->getList(); + + // Carry over any search form settings + $searchContext = BasicSearchContext::create($list->dataClass()); + $searchContext->setFields($defaultSearchFields); + + // Carry over filter configuration (make changes to filter classes so they work with ArrayList data) + foreach ($defaultFilters as $defaultFilter) { + $fieldFilter = PartialMatchFilter::create( + // Use name instead of full name as this plain filter doesn't understand relations + $defaultFilter->getName(), + $defaultFilter->getValue(), + $defaultFilter->getModifiers(), + ); + $searchContext->addFilter($fieldFilter); + } + + $filterHeader->setSearchContext($searchContext); + } + if ($fields->hasTabSet()) { $fields->addFieldToTab('Root.Locales', $gridField); diff --git a/tests/php/Extension/FluentFilteredExtensionTest.php b/tests/php/Extension/FluentFilteredExtensionTest.php index 0d0a9d49..0ea8fe43 100644 --- a/tests/php/Extension/FluentFilteredExtensionTest.php +++ b/tests/php/Extension/FluentFilteredExtensionTest.php @@ -152,7 +152,10 @@ public function testUpdateCMSFields() LocaleToggleColumn::class, $config->getComponentByType(LocaleToggleColumn::class) ); - $this->assertNull($config->getComponentByType(GridFieldFilterHeader::class)); + $this->assertInstanceOf( + GridFieldFilterHeader::class, + $config->getComponentByType(GridFieldFilterHeader::class) + ); }); }