如何在Admin Silverstripe中添加自定义按钮及其功能?

use*_*011 6 html php jquery silverstripe

如何在Admin Silverstripe中添加自定义按钮及其功能?

请告诉我解决方案.

自定义按钮仅在一个菜单中添加.

Tur*_*erj 7

就像评论中提到的@wmk一样,您可以将框架代码GridFieldPrintButton作为基础并从那里开始.SilverStripe还有一个用于创建自定义ActionProvider基本教程.

我将为您提供一个非常基本的自定义操作提供程序,您可以复制和扩展以执行您需要的操作.虽然您没有注意到按钮所需的确切结果,但我将只提供一个非常通用的类.

这段代码是GridFieldPrintButton@wmk提到的精简版本.它支持按钮本身调用自定义代码以及URL.

我在代码中注意到我一直保持"grid-print-button"的引用,这是为了让你的按钮很好地放在打印件旁边而不是可能坐在另一条线上(就像我在测试中一样)我建的旧3.1网站).

class GridFieldCustomButton implements GridField_HTMLProvider, GridField_ActionProvider, GridField_URLHandler {

    protected $targetFragment;
    protected $someCustomConstructData;

    //TargetFragment is just for positioning control of the HTML fragment
    //SomeCustomConstructData is just an example of providing some default options into your butotn
    public function __construct($targetFragment = "after", $someCustomConstructData = null) {
        $this->targetFragment = $targetFragment;
        $this->someCustomConstructData = $someCustomConstructData;
    }

    //Generate the HTML fragment for the GridField
    public function getHTMLFragments($gridField) {
        $button = new GridField_FormAction(
            $gridField,
            'custom',
            'Custom Action',
            'custom',
            null
        );
        return array(
            //Note: "grid-print-button" is used here to match the styling of the buttons in ModelAdmin
            $this->targetFragment => '<p class="grid-print-button">' . $button->Field() . '</p>',
        );
    }

    public function getActions($gridField) {
        return array('myCustomAction');
    }

    public function handleAction(GridField $gridField, $actionName, $arguments, $data) {
        if($actionName == 'myCustomAction') {
            return $this->handleMyCustomAction();
        }
    }

    //For accessing the custom action from the URL
    public function getURLHandlers($gridField) {
        return array(
            'myCustomAction' => 'handleMyCustomAction',
        );
    }

    //Handle the custom action, for both the action button and the URL
    public function handleMyCustomAction($gridField, $request = null) {
        //Do your stuff here!
    }
}
Run Code Online (Sandbox Code Playgroud)

从评论中的讨论继续,您将需要修改自定义ModelAdmin以向其添加新组件GridField.

class MyCustomAdmin extends ModelAdmin
{
    private static $managed_models = array(
        'MyCustomObject' 
    );

    private static $url_segment = 'custom-admin';
    private static $menu_title = 'All Custom Objects';

    public function getEditForm($ID = null, $Fields = null)
    {
        $form = parent::getEditForm($ID, $Fields);
        $fields = $form->Fields();

        $gridField = $fields->fieldByName('MyCustomObject');
        $gridFieldConfig = $gridField->getConfig();
        $gridFieldConfig->addComponent(new GridFieldCustomButton());

        return $form;
    }
}
Run Code Online (Sandbox Code Playgroud)

具体来说,该行$gridFieldConfig->addComponent(new GridFieldCustomButton())完成工作,按照我上面显示的自定义按钮将其添加到ModelAdmin.您还可以GridField通过提供"左前按钮"作为GridFieldCustomButton构造函数中的第一个参数来指定它应该在哪里.

例如. $gridFieldConfig->addComponent(new GridFieldCustomButton("buttons-before-left"))

有关GridField片段的更多信息,请参见SilverStripe开发人员文档.