尝试 remove/hide 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;
}
这是旋转器的代码class:
<?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' 只是为了看看会发生什么,但这些也没有用。我错过了什么?是否可以通过这种方式隐藏自定义选项卡上的字段,或者我是否需要尝试其他方法?
这会起作用...
$fields->removeByName('FieldName');
那么,您想隐藏网格字段详细信息表单中的字段吗?这不能在您的页面 getCMSFields()
中完成,因为网格负责生成详细信息表单。两种可能的解决方案:
1) 告诉网格使用自定义组件隐藏该字段。我不知道该怎么做
2) 告诉您的旋转器 class 仅当相关页面是主页时才显示字段:
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;
}
我正在尝试找出一种方法(如果可能的话)来删除或隐藏自定义选项卡中的某些字段。自定义选项卡标记为 "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;
}
这是旋转器的代码class:
<?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' 只是为了看看会发生什么,但这些也没有用。我错过了什么?是否可以通过这种方式隐藏自定义选项卡上的字段,或者我是否需要尝试其他方法?
这会起作用...
$fields->removeByName('FieldName');
那么,您想隐藏网格字段详细信息表单中的字段吗?这不能在您的页面 getCMSFields()
中完成,因为网格负责生成详细信息表单。两种可能的解决方案:
1) 告诉网格使用自定义组件隐藏该字段。我不知道该怎么做
2) 告诉您的旋转器 class 仅当相关页面是主页时才显示字段:
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;
}