1

我正在尝试在数据对象上的保存和删除按钮旁边添加一个复制数据对象按钮,但“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 字段使复制按钮和下拉字段显示在保存和删除按钮旁边。

4

1 回答 1

4

问题是它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

于 2019-01-31T07:44:35.260 回答