Last active
February 3, 2020 21:24
-
-
Save mooror/6b2a8d71b490c6537e2c850a9b253c0a to your computer and use it in GitHub Desktop.
Model Admin Search Form
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
<?php | |
namespace Company\ProductCatalog\Admin; | |
use Company\ProductCatalog\Model\Product; | |
use Company\ProductCatalog\Model\Vendor; | |
use Company\ProductCatalog\Model\ProductTag; | |
use Company\ProductCatalog\Model\ProductIndustry; | |
use Company\ProductCatalog\Model\Manufacturer; | |
use SilverStripe\Forms\CheckboxField; | |
use SilverStripe\Forms\GridField\GridFieldFilterHeader; | |
use SilverStripe\Admin\ModelAdmin; | |
/** | |
* Description | |
* | |
* @package silverstripe | |
* @subpackage mysite | |
*/ | |
class Admin extends ModelAdmin | |
{ | |
/** | |
* Managed data objects for CMS | |
* @var array | |
*/ | |
private static $managed_models = [ | |
Product::class, | |
Vendor::class, | |
Manufacturer::class, | |
ProductTag::class, | |
ProductIndustry::class, | |
]; | |
/** | |
* URL Path for CMS | |
* @var string | |
*/ | |
private static $url_segment = 'product-catalog'; | |
/** | |
* Menu title for Left and Main CMS | |
* @var string | |
*/ | |
private static $menu_title = 'Product Catalog'; | |
public function getEditForm($id = null, $fields = null) | |
{ | |
$form = parent::getEditForm($id, $fields); | |
$modelClass = $this->modelClass; | |
if($modelClass == ProductIndustry::class) { | |
// $gridFieldName is generated from the ModelClass, eg if the Class 'Product' | |
// is managed by this ModelAdmin, the GridField for it will also be named 'Product' | |
$gridFieldName = $this->sanitiseClassName($modelClass); | |
$gridField = $form->Fields()->fieldByName($gridFieldName); | |
$filterHeader = $gridField->getConfig() | |
->getComponentByType(GridFieldFilterHeader::class); | |
// Add more search fields | |
$context = $filterHeader->getSearchContext($gridField); | |
$context->getFields()->push(new CheckboxField('ParentIndustry', 'Only Parent Industries')); | |
$context->getFields()->push(new CheckboxField('ChildIndustry', 'Only Child Industries')); | |
} | |
return $form; | |
} | |
public function getList() | |
{ | |
// print("Model Class -> ".$this->modelClass); | |
$list = parent::getList(); | |
$filterParam = $this->getRequest()->requestVar('filter'); | |
if($this->modelClass == ProductIndustry::class && $filterParam && isset($filterParam["Company-ProductCatalog-Model-ProductIndustry"])) { | |
$filterNamespaceParam = $filterParam["Company-ProductCatalog-Model-ProductIndustry"]; | |
if (isset($filterNamespaceParam["ParentIndustry"]) && $filterNamespaceParam["ParentIndustry"]) { | |
// print("ParentIndustry is set to -> ".$parentIndustryParam); | |
$list = $list->exclude('ParentIndustryID:not', '0'); | |
} else if (isset($filterNamespaceParam["ChildIndustry"]) && $filterNamespaceParam["ChildIndustry"]) { | |
// print("ChildIndustry is set to -> ".$childIndustryParams); | |
$list = $list->exclude('ParentIndustryID', '0'); | |
} | |
} | |
return $list; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment