Created
August 29, 2021 22:09
-
-
Save unclecheese/d8fdc130d878c50d68f0374c1042b6bf to your computer and use it in GitHub Desktop.
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 | |
class ModelAdminFilteredViewExtension extends DataExtension | |
{ | |
public function updateList(SS_List &$list) | |
{ | |
$list = $list->filter($this->getFilters()); | |
} | |
public function updateEditForm(Form &$form) | |
{ | |
$filters = $this->getFilters(); | |
$grid = null; | |
foreach($form->Fields() as $f) { | |
if($f instanceof GridField) { | |
$grid = $f; | |
break; | |
} | |
} | |
if(!$grid) return $form; | |
$grid->getConfig()->getComponentByType('GridFieldDetailForm') | |
->setItemEditFormCallback(function ($detailForm) use ($filters) { | |
foreach($filters as $name => $val) { | |
$detailForm->Fields()->push( | |
HiddenField::create($name, null, $val) | |
); | |
} | |
}); | |
} | |
protected function getFilters() | |
{ | |
if($this->owner->hasMethod('getModelAdminFilters')) { | |
return $this->owner->getModelAdminFilters(); | |
} | |
return $this->owner->config()->filters; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment