添加一个按钮到CMS在SilverStripe [英] Adding a button to the CMS in SilverStripe

查看:124
本文介绍了添加一个按钮到CMS在SilverStripe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何添加一个按钮,CMS的将触发动作的后台?我可以显示我想要使用的按钮:

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()
{
    $fields = parent::getCMSFields();

    $fields->addFieldsToTab("Root.ButtonTest", array(
            FormAction::create('doAction', 'Action button')
        )
    );

    return $fields;
}

public function doAction() 
{
   //Do something
}

然而,当点击添加按钮,什么也不做。

However the button added does nothing when clicked.

我已经看到了如何把主要的操作栏上的按钮(Next保存/发布)的一个例子,但是这不是我想要做的。

I've seen one example of how to put a button on the main action bar (next to save/publish) but that's not what I'm trying to do.

看着唯一的<一个href=\"http://web.archive.org/web/20140817055240/http://doc.silverstripe.org/framework/en/trunk/howto/extend-cms-interface\"相对=nofollow> 我能找到的文档的页面,做我需要做的事之内:

Looking at the only page of documentation I can find, do I need to do something within:

public function getCMSActions()
{
    $actions = parent::getCMSActions();
    //Something here?
}

这不是很清楚如何创建操作的按钮调用。

It isn't very clear how to create the action that the button calls.

推荐答案

您将不得不延长/装饰 LeftAndMain 用自己的分机,你要拨打的行动。这里有一个例子:

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

<?php
class MyExtension extends LeftAndMainExtension
{
    private static $allowed_actions = array(
        'doAction'  
    );

    public function doAction($data, $form){
        $className = $this->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);
    }
}

一旦你这样写的一个扩展,你必须通过添加以下到你的 mysite的/ _config /把它应用到 LeftAndMain config.yml

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

就是这样。你的 doAction 按钮现在实际上应该做点什么!

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

这篇关于添加一个按钮到CMS在SilverStripe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆