我试图找出一种方法(如果可能的话)删除或隐藏自定义标签中的某些字段。自定义选项卡标有" 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&#39;和&#39; Root.Rotator.Content&#39;只是看看会发生什么,那些也没有用。我错过了什么?是否可以通过这种方式隐藏自定义选项卡上的字段,还是需要尝试其他内容?
答案 0 :(得分: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;
}
答案 1 :(得分:1)
这会有用......
$fields->removeByName('FieldName');