【问题标题】:Adding a button to the CMS in SilverStripe在 SilverStripe 中向 CMS 添加按钮
【发布时间】:2014-06-14 18:16:38
【问题描述】:

如何在 CMS 的后端添加一个按钮来触发操作?我可以在我想使用的地方显示按钮:

public function getCMSFields()
{
    $fields = parent::getCMSFields();

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

    return $fields;
}

public function doAction() 
{
   //Do something
}

但是添加的按钮在点击时什么都不做。

我见过一个如何在主操作栏(保存/发布旁边)上放置按钮的示例,但这不是我想要做的。

看着我能找到的唯一page of documentation,我需要在里面做点什么吗:

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

目前还不清楚如何创建按钮调用的动作。

【问题讨论】:

  • 您希望按钮“做什么”?是否可以选择呈现一个复选框并在“onBeforeWrite”钩子中对其做出反应?
  • 并非如此,该按钮将向第三方服务发送请求,检查响应并将其显示给用户。理想情况下无需重新加载页面。在模糊事件或其他东西上使用一点 js 可能会更容易。
  • 文档管理器系统模块可以做到这一点,但我对 SS 的了解还不够多,无法对他们是如何做到的进行逆向工程。

标签: button content-management-system silverstripe


【解决方案1】:

您必须使用自己的扩展名和您要调用的操作来扩展/装饰LeftAndMain。这是一个例子:

<?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/config.yml 来将其应用到 LeftAndMain:

LeftAndMain:
  extensions:
    - MyExtension

就是这样。你的doAction 按钮现在应该可以做点什么了!

【讨论】:

  • 非常有意义 - 谢谢!
  • doAction 不需要在$allowed_actions,从Form-&gt;httpSubmission调用
【解决方案2】:

不确定这是否有帮助,但您可以通过以下方式将操作按钮添加到 ModelAdmin。
(是否重新加载页面)

...在管理类中:

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 } 

}

【讨论】:

  • 感谢您花时间回复,但它并没有真正给我一个明确的答案。不幸的是,这似乎是所有人都想做的事情,谷歌在这种情况下的例子中都有介绍,但在其他情况下没有。
  • 如果你指的是 DMS 模块中的“添加文档”按钮,你可能想看看这个:doc.silverstripe.org/framework/en/reference/…(我认为它在那里使用了什么......)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-10
  • 1970-01-01
  • 2016-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多