问题描述
如何在Admin Silverstripe中添加自定义按钮及其功能?
请告诉我解决方案。
自定义按钮只能添加到一个菜单中。
这里不提供教程,我会为您提供一个非常基本的自定义操作提供程序,您可以将其复制并扩展到做你需要的。虽然你没有注意到你想从按钮中得到的确切结果,但我会提供一个非常通用的类。
这个代码是一个精简版的 GridFieldPrintButton @wmk提到。它支持按钮本身调用自定义代码以及URL。
我在代码中注意到我一直在使用grid-print-按钮,这是为了让你的按钮很好地坐在印刷品旁边,而不是坐在另一条线上(就像我在我建立的旧3.1网站上测试过的那样)。
class GridFieldCustomButton实现了GridField_HTMLProvider,GridField_ActionProvider,GridField_URLHandler {
protected $ targetFragment;
保护$ someCustomConstructData;
// TargetFragment仅用于对HTML片段进行定位控制
// SomeCustomConstructData只是向butotn提供一些默认选项的一个示例
public function __construct($ targetFragment = after后,$ someCustomConstructData = null){
$ this-> targetFragment = $ targetFragment;
$ this-> someCustomConstructData = $ someCustomConstructData;
}
//为GridField生成HTML片段
public function getHTMLFragments($ gridField){
$ button = new GridField_FormAction(
$ gridField ,
'custom',
'Custom Action',
'custom',
null
);
返回数组(
//注意:此处使用grid-print-button来匹配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();
//用于从URL访问自定义动作
public function getURLHandlers($ gridField){
return array(
'myCustomAction'=>'handleMyCustomAction',
);
}
//处理动作按钮和URL的自定义动作
public function handleMyCustomAction($ gridField,$ request = null){
/ /在这里做你的东西!
}
}
继续从评论的讨论中,您将需要修改您的自定义 ModelAdmin 以将新组件添加到其 GridField 。
class MyCustomAdmin extends ModelAdmin
{
private static $ managed_models = array(
'MyCustomObject'
);
private static $ url_segment ='custom-admin';
private static $ menu_title ='所有自定义对象';
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;
$ b特别是,$ $ $ gridFieldConfig-> addComponent(new GridFieldCustomButton())完成这项工作,将您的自定义按钮添加到 ModelAdmin 。您还可以通过在 GridFieldCustomButton $>中提供buttons-before-left作为第一个参数,指定它应该放在 GridField 中的位置c $ c>构造函数。
例如。 $ gridFieldConfig-> addComponent(new GridFieldCustomButton(after-left-left))
GridField 片段可以在。
How to add Custom button and its functionality in Admin Silverstripe?
Please tell me solution.
Custom Button add only in one menu.
解决方案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.
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.
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).
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.
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.
eg. $gridFieldConfig->addComponent(new GridFieldCustomButton("buttons-before-left"))
More information regarding GridField fragments can be found in the SilverStripe developer documentation.
这篇关于如何在Admin Silverstripe中添加自定义按钮及其功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!