Skip to content

Instantly share code, notes, and snippets.

@unclecheese
Created August 29, 2021 22:09
Show Gist options
  • Save unclecheese/d8fdc130d878c50d68f0374c1042b6bf to your computer and use it in GitHub Desktop.
Save unclecheese/d8fdc130d878c50d68f0374c1042b6bf to your computer and use it in GitHub Desktop.
<?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