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

标签 php jquery html silverstripe

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

请告诉我解决方案。

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

最佳答案

就像评论中提到的@wmk 一样,您可以只以GridFieldPrintButton 的框架代码为基础,然后从那里开始。 SilverStripe 还有一个 basic tutorial for creating a custom ActionProvider .

我不会在这里重述教程,而是会为您提供一个非常基本的自定义操作提供程序,您可以复制和扩展它来执行您需要的操作。虽然您没有注意到您希望从按钮获得的确切结果,但我将只提供一个非常通用的类。

此代码是@wmk 提到的 GridFieldPrintButton 的精简版本。它支持按钮本身调用自定义代码以及 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!
    }
}

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

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

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

有关 GridField 片段的更多信息可以在 SilverStripe developer documentation 中找到.

关于php - 如何在 Admin Silverstripe 中添加自定义按钮及其功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32140399/

相关文章:

php - laravel 组件类被忽略

php - 输入类型密码值?

php - 每 30 天范围内的列计数

javascript - toast 通知超过 'overlay'

jquery - 单击外部 div 后关闭覆盖

php - 使用jquery检测表单提交完成

javascript - jQuery 的 ajax 和 $.post() 向自身发出请求后重复的 div

javascript - 仅在第一次执行函数 onload

javascript - PDF 嵌入在当前代码的 IE 上不起作用

javascript - IE 中的 CSS 文本框呈现问题