【问题标题】:Trying to remove/hide fields in custom tab in SilverStripe尝试在 SilverStripe 的自定义选项卡中删除/隐藏字段
【发布时间】:2015-11-27 23:23:56
【问题描述】:

我正在尝试找出一种方法(如果可能)来删除或隐藏自定义选项卡中的某些字段。自定义选项卡被标记为“Rotator”,它包含可用于页面上的旋转横幅的图像。主页横幅有点不同,因为它有两个子页面不需要的额外字段: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.Content' 只是想看看会发生什么,但这些也没有用。我错过了什么?是否可以通过这种方式隐藏自定义选项卡上的字段,还是我需要尝试其他方法?

【问题讨论】:

  • 您的字段“Body”和“BackgroundImage”添加到哪里?在数据扩展中?您还可以在添加页面时检查页面的类...
  • “Body”和“BackgroundImage”字段位于一个类中。我在上面发布了它的代码

标签: php silverstripe


【解决方案1】:

这会起作用的......

$fields->removeByName('FieldName');

【讨论】:

    【解决方案2】:

    好吧,您想隐藏网格字段详细信息表单中的字段吗?这无法在您的页面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;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-04-08
      • 1970-01-01
      • 1970-01-01
      • 2014-07-03
      • 1970-01-01
      • 2016-11-24
      • 2013-11-26
      • 1970-01-01
      • 2013-08-03
      相关资源
      最近更新 更多