When managing Dataobject
s with ModelAdmin
in Silverstripe 3, i was wondering how you can add buttons for custom action to the list view as well as to the edit-page.
I've managed to add a button for a action to the edit-page by setting the ItemRequest
on the ModelAdmin
s GridField
class with the code below:
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);
}
}
}
So the question is:
Can i have a button for the same action in the ModelAdmin
GridField
Listview?
So that it appears next to the edit and delete button: