3
當在Silverstripe 3中管理Dataobject
s和ModelAdmin
時,我想知道如何將自定義操作的按鈕添加到列表視圖以及編輯頁面。將自定義操作的按鈕添加到ModelAdmin
我已經成功地添加一個按鈕的動作到編輯頁面用下面的代碼在ModelAdmin
小號GridField
類設置ItemRequest
:
class MyModelAdmin extends ModelAdmin
{
//...
public function getEditForm($id = null, $fields = null)
{
$form = parent::getEditForm($id, $fields);
$form
->Fields()
->fieldByName($this->sanitiseClassName($this->modelClass))
->getConfig()
->getComponentByType('GridFieldDetailForm')
->setItemRequestClass('MyModelGridFieldDetailForm_ItemRequest');
return $form;
}
}
MyModelGridFieldDetailForm_ItemRequest.php
class MyModelGridFieldDetailForm_ItemRequest extends GridFieldDetailForm_ItemRequest
{
function ItemEditForm()
{
$form = parent::ItemEditForm();
$formActions = $form->Actions();
$button = FormAction::create('myAction');
$button->setTitle('My Custom Action');
$button->addExtraClass('ss-ui-action-constructive');
$formActions->push($button);
$form->setActions($formActions);
return $form;
}
function myAction($data, $form)
{
//do things
$form->sessionMessage('My Action has been successful', 'good');
if ($this->gridField->getList()->byId($this->record->ID)) {
return $this->edit(Controller::curr()->getRequest());
} else {
$noActionURL = Controller::curr()->removeAction($data['url']);
Controller::curr()->getRequest()->addHeader('X-Pjax', 'Content');
return Controller::curr()->redirect($noActionURL, 302);
}
}
}
所以問題是:
我可以有一個按鈕爲同樣動作在ModelAdmin
GridField
Listview? 因此,它旁邊會出現編輯和刪除按鈕:
這樣做 - 謝謝:)我是一種像getCMSActions()我希望有一種神奇的方法,我只需要重寫,以改變這兩個領域... –