Silverstripe 4.0.4-getCMSActions在dataobject上无法按预期工作

时间:2019-01-18 09:38:49

标签: php backend silverstripe silverstripe-4 data-objects

我试图在数据对象上的保存和删除按钮旁边添加一个复制数据对象按钮,但是“ getCMSActions”似乎不起作用。

我已遵循以下页面上的教程:

https://docs.silverstripe.org/en/4/developer_guides/customising_the_admin_interface/how_tos/extend_cms_interface/#extending-the-cms-actions

https://docs.silverstripe.org/en/4/developer_guides/customising_the_admin_interface/how_tos/cms_alternating_button/

但是两个都没有解决我的问题,我的代码当前看起来像这样。

public function getCMSActions() {
    $actions = parent::getCMSActions();

    if ($this->ID) {
        $actions->push(FormAction::create('copy', _t('SiteBlockAdmin.Copy', 'Copy'))
            ->setUseButtonTag(true)
            ->setAttribute('data-icon', 'arrow-circle-double'));
        $actions->push(DropdownField::create('BegrotingsPageCopyToID', '', BegrotingsPage::get()->map())
            ->setEmptyString('Selecteer pagina voor kopie'));
    }       

    return $actions;
}

我想要实现的是使复制按钮和下拉字段显示在带有getCMSActions字段的保存和删除按钮旁边。

1 个答案:

答案 0 :(得分:2)

问题在于GridFieldDetailForm_ItemRequest::getFormActions()不会调用$this->record->getCMSActions(),而是将其初始动作列表定义为$actions = new FieldList();

我假设您正在通过ModelAdmin管理DataObject。

您可以向此类添加扩展,并以这种方式添加字段(但它不是最优的):

# File: app/_config/extensions.yml
SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest:
  extensions:
    MyExtension: MyExtension

您的扩展程序可能如下所示:

<?php

use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\FormAction;
use SilverStripe\ORM\DataExtension;

class MyExtension extends DataExtension
{
    public function updateFormActions(FieldList $actions)
    {
        $record = $this->owner->getRecord();
        // This extension would run on every GridFieldDetailForm, so ensure you ignore contexts where
        // you are managing a DataObject you don't care about
        if (!$record instanceof YourDataObject || !$record->exists()) {
            return;
        }

        $actions->push(FormAction::create('copy', _t('SiteBlockAdmin.Copy', 'Copy'))
            ->setUseButtonTag(true)
            ->setAttribute('data-icon', 'arrow-circle-double'));
        $actions->push(DropdownField::create('BegrotingsPageCopyToID', '', BegrotingsPage::get()->map())
            ->setEmptyString('Selecteer pagina voor kopie'));
    }
}

我也提出了一个问题,以跟进具有误导性的文档:https://github.com/silverstripe/silverstripe-framework/issues/8773