Skip to content

Commit

Permalink
ENH: Localisation manager filter.
Browse files Browse the repository at this point in the history
  • Loading branch information
mfendeksilverstripe committed Jan 22, 2025
1 parent 7a04530 commit 1159c29
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
37 changes: 35 additions & 2 deletions src/Extension/Traits/FluentObjectTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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);

Expand Down
5 changes: 4 additions & 1 deletion tests/php/Extension/FluentFilteredExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
});
}

Expand Down

0 comments on commit 1159c29

Please sign in to comment.