将自定义操作的按钮添加到ModelAdmin

时间:2013-08-21 05:21:00

标签: php silverstripe

在Silverstripe 3中使用Dataobject管理ModelAdmin时,我想知道如何将自定义操作按钮添加到列表视图以及编辑页面。

我已设法通过使用以下代码设置ItemRequest s ModelAdmin类的GridField,为编辑页面添加操作按钮:

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列表视图中找到相同操作的按钮吗? 因此它出现在编辑和删除按钮旁边:

ModelAdmin GridField actions

1 个答案:

答案 0 :(得分:6)

您可以通过创建自己的GridFieldComponent实施GridField_ColumnProviderGridField_ActionProvider来完成此操作。

在您的情况下,您几乎可以复制GridFieldDeleteAction课程并使用自己的getColumnContent()修改GridField_FormAction并修改handleAction()以实现您的魔力。