2015-08-21 33 views
6

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

請告訴我解決方案。

自定義按鈕僅在一個菜單中添加。

+0

基本上都在你的屏幕截圖打印按鈕就是一個很好的例子:見https://github.com/silverstripe/silverstripe-framework/blob/6a45f4a1e125b1a75d042e59b38824b24fd3cd0f/forms/gridfield/GridFieldPrintButton.php – wmk

+0

感謝您的回覆。 @wmk你能告訴我,我必須做出新的文件,或者只需要編輯相同的文件來製作新的按鈕。 – user3111011

回答

5

就像評論中提到的@wmk一樣,您可以將GridFieldPrintButton的框架代碼作爲基礎並從那裏開始。 SilverStripe也有basic tutorial for creating a custom ActionProvider

我不會在這裏重溫本教程,我會爲您提供一個非常基本的自定義操作提供程序,您可以複製和擴展以執行所需的操作。雖然你沒有注意到你想從按鈕中得到的確切結果,但我會提供一個非常通用的類。

此代碼是@wmk提到的GridFieldPrintButton的精簡版。它支持按鈕本身調用自定義代碼以及URL。

我已經在代碼中指出,我一直爲「網格式打印按鈕」的引用,這是爲了讓你的按鈕很好地坐在旁邊的打印,而不是坐在可能性另一線路(因爲它沒有在我在我建立的舊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自定義按鈕。您還可以指定過通過提供「按鈕,前左」作爲GridFieldCustomButton構造函數的第一個參數,它應該在GridField去。

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

有關GridField片段的更多信息,請參見SilverStripe developer documentation

+0

感謝您的回覆。但我問同樣的問題,我問他作爲wynk的方式,你可以告訴我,我必須做出新的文件或只需要編輯相同的文件,使新的按鈕。? – user3111011

+1

你必須創建一個新的文件,並將其命名爲喜歡你的PHP類(所以自動裝卸發現它) – wmk

+0

我只在一個菜單即讓一個新的按鈕只顯示在公司見截圖。並在網格域文件夾中創建一個新文件? – user3111011

相關問題