Created
April 7, 2020 06:23
-
-
Save jkersu/f575e888273c46b51aa4115f2dda4d70 to your computer and use it in GitHub Desktop.
ModelAdmin Search Filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static $searchable_fields = [ | |
'Title' => [ | |
'title' => 'Title' | |
], | |
'Year' => [ | |
'title' => 'Year', | |
'field' => DropdownField::class | |
], | |
'ProductType.Title' => [ | |
'title' => 'Product Type', | |
'field' => DropdownField::class | |
], | |
]; | |
/** | |
* @return SearchContext | |
*/ | |
public function getDefaultSearchContext() | |
{ | |
$fields = $this->scaffoldSearchFields(); | |
$filters = [ | |
'Title' => new PartialMatchFilter('Title'), | |
'Year' => new ExactMatchFilter('Year'), | |
'ProductType.Title' => new ExactMatchFilter('ProductType.Title'), | |
]; | |
$yearField = $fields->dataFieldByName('Year'); | |
if ($yearField) { | |
$yearField->setSource($validYears)->setEmptyString(''); | |
} | |
$productTypes = ProductType::get()->map('Title', 'Title'); | |
$productTypesField = $fields->dataFieldByName('ProductType__Title'); | |
if ($productTypesField) { | |
$productTypesField->setSource($productTypes)->setEmptyString(''); | |
} | |
return new SearchContext( | |
WhateverDataObjectClass::class, | |
$fields, | |
$filters | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment