I am managing a DataObject
via ModelAdmin
and I have added a custom button because I want to do a custom action when adding the item.
class InventoryAdmin extends ModelAdmin {
public function getEditForm($id = null, $fields = null) {
$form = parent::getEditForm($id, $fields);
$listField = $form->Fields()->fieldByName($this->modelClass);
if ($gridField = $listField->getConfig()->getComponentByType('GridFieldDetailForm'))
$gridField->setItemRequestClass('InventoryFieldDetailForm_ItemRequest');
return $form;
}
}
class InventoryFieldDetailForm_ItemRequest extends GridFieldDetailForm_ItemRequest {
private static $allowed_actions = array (
'edit',
'view',
'ItemEditForm'
);
public function ItemEditForm() {
$form = parent::ItemEditForm();
$formActions = $form->Actions();
$formActions->removeByName('action_doSave');
if ($actions = $this->record->getCMSActions()) {
foreach ($actions as $action) {
$formActions->push($action);
}
}
return $form;
}
public function doAddInventoryItem($data, $form) {
return Controller::curr()->redirect($this->getToplevelController()->Link());
}
}
This works however on submission the CMS goes back to the parent and it's offset by about 15 px. The main issue is that the CMS UI becomes unresponsive from then on until a refresh.
What should I be redirecting to to get to the parent ModelAdmin
?
GridFieldDetailForm_ItemRequest
class has the function getBacklink
which should provide a link to the previous screen.
You can then use getToplevelController
to get the controller and set the following header to force a content refresh.
$controller = $this->getToplevelController();
$controller->getRequest()->addHeader('X-Pjax', 'Content'); // Force a content refresh
return $controller->redirect($this->getBacklink(), 302); //redirect back to admin section