试图删除/隐藏字段自定义选项卡在SilverStripe


Trying to remove/hide fields in custom tab in SilverStripe

我正试图找出一种方法(如果可能的话)来删除或隐藏自定义选项卡中的某些字段。自定义选项卡被标记为"Rotator",它包含可用于页面上旋转横幅的图像。首页横幅有点不同,因为它有2个额外的子页面上不需要的字段:BackgroundImage和Body(用于保存各种文本)。我想让内容管理器简单一些,所以我想在子页面上隐藏这些字段。

我知道removeFieldFromTab和它是如何工作的,我正在考虑在page .php文件上使用它(因为这基本上是我的SilverStripe文件中所有页面类型的主要模板):

  public function getCMSFields() {
    $fields = parent::getCMSFields();
    $gridFieldConfig = GridFieldConfig_RecordEditor::create();
    $gridFieldConfig->addComponent(new GridFieldBulkImageUpload());
    $gridFieldConfig->addComponent(new GridFieldSortableRows('SortOrder'));
    $gridFieldConfig->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
        // field from drawer class => label in UI
        'ID' => 'ID',
        'Title' => 'Title',
        'Thumbnail' => 'Thumbnail',
        'InternalURL.Link' => 'Internal URL',
    ));
    $gridfield = new GridField(
        "Rotator",
        "Rotator",
        $this->Rotator()->sort("SortOrder"),
        $gridFieldConfig
    );
    $fields->addFieldToTab('Root.Rotator', $gridfield);
    $fields->addFieldToTab("Root.Main", new TextField("H1"), "Content");
    $fields->addFieldToTab("Root.Main", new TextField("Subheader"), "Content");
    $fields->addFieldToTab('Root.Main', new TextField('PageTitle', 'Page Title'), 'MetaDescription');
    $fields->removeFieldFromTab('Root.Rotator', 'Body');
    $fields->removeFieldFromTab('Root.Rotator', 'BackgroundImage');
    return $fields;
}
下面是Rotator类的代码:
<?php
class RotatorImage extends DataObject {
    public static $db = array(
        'SortOrder' => 'Int',
        'Header' => 'varchar',
        'Body' => 'HTMLText',
    );
    // One-to-one relationship with gallery page
    public static $has_one = array(
        'Image' => 'Image',
        'BackgroundImage' => 'Image',
        'Page' => 'Page',
        'InternalURL' => 'SiteTree',
    );
    // tidy up the CMS by not showing these fields
    public function getCMSFields() {
        $fields = parent::getCMSFields();
        $fields->removeFieldFromTab("Root.Main","PageID");
        $fields->removeFieldFromTab("Root.Main","SortOrder");
        return $fields;
    }
    // Tell the datagrid what fields to show in the table
    public static $summary_fields = array(
        'ID' => 'ID',
        'Title' => 'Title',
        'Thumbnail' => 'Thumbnail',
        'InternalURLID' => 'Internal URL',
    );
    // this function creates the thumnail for the summary fields to use
    public function getThumbnail() {
        return $this->Image()->CMSThumbnail();
    }
    public function canEdit() {
        return true;
    }
    public function canDelete() {
        return true;
    }
    public function canCreate(){
        return true;
    }
    public function canPublish(){
        return true;
    }
    public function canView(){
        return true;
    }
}

然而,这不起作用,我确信我有正确的字段名称。我试过"Root.Rotator"。Main和root。rotator。内容只是看看会发生什么,那些也没有工作。我错过了什么?是否可以通过这种方式隐藏自定义选项卡上的字段,或者我需要尝试其他方法?

那么,您想隐藏网格字段详细信息表单中的字段吗?这在您的页面getCMSFields()中无法完成,因为网格负责生成详细表单。两种可能的解决方案:

1)告诉网格用自定义组件隐藏字段。我不知道该怎么做

2)告诉你的Rotator类只在相关页面是主页时显示字段:

public function getCMSFields() {
    $fields = parent::getCMSFields();
    //...other stuff....
    $isOnHomePage = ($this->Page() && $this->Page()->ClassName == 'HomePage'); //put in your own classname or conditions
    if(!$isOnHomePage) {
        //remove the fields if you're not on the HomePage
        $fields->removeByName('Body');
        //we need to suffix with "ID" when we have a has_one relation!
        $fields->removeByName('BackGroundImageID'); 
    }
    return $fields;
}

这将工作…

$fields->removeByName('FieldName');