简体   繁体   English

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

[英]Silverstripe 4.0.4 - getCMSActions not working as expected on dataobject

I am trying to add a copy dataobject button next to the save and delete button on a dataobject but "getCMSActions" does not seem to work. 我正在尝试在数据对象上的保存和删除按钮旁边添加一个复制数据对象按钮,但是“ getCMSActions”似乎不起作用。

I have followed the tutorials on the following pages: 我遵循了以下页面上的教程:

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/extend_cms_interface/#extending-the-cms-actions

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

But both did not solve my problem my code currently looks like this. 但是两者都没有解决我的问题,我的代码当前看起来像这样。

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;
}

What I want to achieve is to make the copy button and dropdownfield show up next to the save and delete button with the getCMSActions field. 我要实现的是使复制按钮和dropdownfield出现在带有getCMSActions字段的保存和删除按钮旁边。

The problem is that GridFieldDetailForm_ItemRequest::getFormActions() doesn't call $this->record->getCMSActions() , instead it defines its initial list of actions as $actions = new FieldList(); 问题在于GridFieldDetailForm_ItemRequest::getFormActions()不会调用$this->record->getCMSActions() ,而是将其初始动作列表定义为$actions = new FieldList(); .

I assume you're managing your DataObject via a ModelAdmin. 我假设您正在通过ModelAdmin管理DataObject。

You can add an extension to this class and add the fields that way (but it's sub-optimal): 您可以向此类添加扩展,并以这种方式添加字段(但它不是最优的):

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

And your extension could look like this: 您的扩展名可能如下所示:

<?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'));
    }
}

I've also raised an issue to follow up on the misleading documentation: https://github.com/silverstripe/silverstripe-framework/issues/8773 我还提出了一个问题,以跟进具有误导性的文档: https : //github.com/silverstripe/silverstripe-framework/issues/8773

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM