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

ENH: Localisation manager filter. #922

Open
wants to merge 1 commit into
base: 7
Choose a base branch
from
Open
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
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
Loading