Adding a button to the CMS in SilverStripe

后端 未结 2 1016
日久生厌
日久生厌 2020-12-19 12:28

How do I add a button to the backend of the CMS that fires an action? I can display the button where I want using:

public function getCMSFields()
{
    $fiel         


        
2条回答
  •  天涯浪人
    2020-12-19 12:52

    Not sure if this is helpful, but here's how you can add action-buttons to a ModelAdmin.
    (does reload the page)

    ...in the admin class:

    public function getEditForm($id = null, $fields = null)
    {
        $form = parent::getEditForm($id, $fields);
        $form
            ->Fields()
            ->fieldByName($this->sanitiseClassName($this->modelClass))
            ->getConfig()
            ->getComponentByType('GridFieldDetailForm')
            ->setItemRequestClass('MyGridFieldDetailForm_ItemRequest');
    
        return $form;
    }
    

    MyGridFieldDetailForm_ItemRequest.php

    class MyGridFieldDetailForm_ItemRequest extends GridFieldDetailForm_ItemRequest
    {
        function ItemEditForm()
        {
            $form = parent::ItemEditForm();
            $formActions = $form->Actions();
    
            $button = FormAction::create('myAction');
            $button->setTitle('button label');
            $button->addExtraClass('ss-ui-action-constructive');
            $formActions->push($button);
    
    
            $form->setActions($formActions);
            return $form;
        }
    
        public function myAction(){ //do things } 
    
    }
    

提交回复
热议问题