Adding a button to the CMS in SilverStripe

后端 未结 2 1023
日久生厌
日久生厌 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:51

    You'll have to extend/decorate LeftAndMain with your own extension and the action you want to call. Here's an example:

    owner->stat('tree_class');
            $SQL_id = Convert::raw2sql($data['ID']);
    
            $record = DataObject::get_by_id($className, $SQL_id);
    
            if(!$record || !$record->ID){
                throw new SS_HTTPResponse_Exception(
                    "Bad record ID #" . (int)$data['ID'], 404);
            }
    
            // at this point you have a $record, 
            // which is your page you can work with!
    
            // this generates a message that will show up in the CMS
            $this->owner->response->addHeader(
                'X-Status',
                rawurlencode('Success message!') 
            );
    
            return $this->owner->getResponseNegotiator()
                   ->respond($this->owner->request);
        }
    }
    

    Once you have written an extension like this, you'll have to apply it to LeftAndMain by adding the following to your mysite/_config/config.yml:

    LeftAndMain:
      extensions:
        - MyExtension
    

    That's it. Your doAction button should now actually do something!

提交回复
热议问题