如何添加自定义按钮及其功能在管理银条纹?

时间:2023-01-19 16:49:33

How to add Custom button and its functionality in Admin Silverstripe?

如何添加自定义按钮及其功能在管理银条纹?

Please tell me solution.

请告诉我解决方案。

Custom Button add only in one menu.如何添加自定义按钮及其功能在管理银条纹?

自定义按钮只在一个菜单中添加。

1 个解决方案

#1


5  

Like @wmk mentioned in the comments, you can just take the framework code for GridFieldPrintButton as a base and go from there. SilverStripe also have a basic tutorial for creating a custom ActionProvider.

就像在评论中提到的@wmk一样,您可以将GridFieldPrintButton的框架代码作为一个基础,然后从那里开始。SilverStripe还提供了创建自定义ActionProvider的基本教程。

Rather than rehash the tutorial here, I will provide you a very basic custom action provider that you can copy and extend to do what you need. While you don't note the exact result you are wanting from the button, I will provide just a very generic class.

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

This code is a stripped down version of the GridFieldPrintButton that @wmk mentioned. It supports both the button itself invoking the custom code as well as the URL.

这段代码是@wmk提到的GridFieldPrintButton的简化版本。它既支持按钮本身调用自定义代码,也支持URL。

I've noted in the code a reference that I have kept to "grid-print-button", this is to make your button sit nicely next to the print rather than likely sitting on another line (as it did in my testing on an older 3.1 site I built).

我在代码中注意到一个我一直保存在“网格-打印-按钮”的引用,这是为了让您的按钮很好地放在打印旁边,而不是放在另一行(就像我在以前的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!
    }
}

Continuing on from the discussion in the comments, you will need to modify your custom ModelAdmin to add new components to its GridField.

继续从注释中的讨论,您将需要修改自定义的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;
    }
}

Specifically, the line $gridFieldConfig->addComponent(new GridFieldCustomButton()) does the work, taking your custom button as I have shown above and added it to the ModelAdmin. You can also specify where it should go in the GridField too by providing "buttons-before-left" as the first argument in the GridFieldCustomButton constructor.

具体来说,line $gridFieldConfig->addComponent(新的GridFieldCustomButton())完成了工作,按我上面显示的自定义按钮,并将它添加到ModelAdmin中。您还可以通过在GridFieldCustomButton构造函数中提供“左前按钮”作为第一个参数来指定它在GridField中的位置。

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

如。$ gridFieldConfig - > addComponent(新GridFieldCustomButton(“buttons-before-left”))

More information regarding GridField fragments can be found in the SilverStripe developer documentation.

关于GridField片段的更多信息可以在SilverStripe开发人员文档中找到。

#1


5  

Like @wmk mentioned in the comments, you can just take the framework code for GridFieldPrintButton as a base and go from there. SilverStripe also have a basic tutorial for creating a custom ActionProvider.

就像在评论中提到的@wmk一样,您可以将GridFieldPrintButton的框架代码作为一个基础,然后从那里开始。SilverStripe还提供了创建自定义ActionProvider的基本教程。

Rather than rehash the tutorial here, I will provide you a very basic custom action provider that you can copy and extend to do what you need. While you don't note the exact result you are wanting from the button, I will provide just a very generic class.

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

This code is a stripped down version of the GridFieldPrintButton that @wmk mentioned. It supports both the button itself invoking the custom code as well as the URL.

这段代码是@wmk提到的GridFieldPrintButton的简化版本。它既支持按钮本身调用自定义代码,也支持URL。

I've noted in the code a reference that I have kept to "grid-print-button", this is to make your button sit nicely next to the print rather than likely sitting on another line (as it did in my testing on an older 3.1 site I built).

我在代码中注意到一个我一直保存在“网格-打印-按钮”的引用,这是为了让您的按钮很好地放在打印旁边,而不是放在另一行(就像我在以前的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!
    }
}

Continuing on from the discussion in the comments, you will need to modify your custom ModelAdmin to add new components to its GridField.

继续从注释中的讨论,您将需要修改自定义的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;
    }
}

Specifically, the line $gridFieldConfig->addComponent(new GridFieldCustomButton()) does the work, taking your custom button as I have shown above and added it to the ModelAdmin. You can also specify where it should go in the GridField too by providing "buttons-before-left" as the first argument in the GridFieldCustomButton constructor.

具体来说,line $gridFieldConfig->addComponent(新的GridFieldCustomButton())完成了工作,按我上面显示的自定义按钮,并将它添加到ModelAdmin中。您还可以通过在GridFieldCustomButton构造函数中提供“左前按钮”作为第一个参数来指定它在GridField中的位置。

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

如。$ gridFieldConfig - > addComponent(新GridFieldCustomButton(“buttons-before-left”))

More information regarding GridField fragments can be found in the SilverStripe developer documentation.

关于GridField片段的更多信息可以在SilverStripe开发人员文档中找到。