我有一个被调用的DataObject ContentSection,它有2个has_one关系:到页面类型LandingPage和另一个DataObject Person.
class ContentSection extends DataObject {
protected static $has_one = array(
'Person' => 'Person',
'LandingPage' => 'LandingPage'
);
}
Run Code Online (Sandbox Code Playgroud)
LandingPage和Person都定义了与ContentSection的has_many关系.
class LandingPage extends Page {
protected static $has_many = array(
'ContentSections' => 'ContentSection'
);
}
class Person extends DataObject {
protected static $has_many = array(
'ContentSections' => 'ContentSection'
);
}
Run Code Online (Sandbox Code Playgroud)
ContentSections可以通过LandingPage和Person编辑,GridFieldConfig_RelationEditor例如:
function getCMSFields() {
$fields = parent::getCMSFields();
$config = GridFieldConfig_RelationEditor::create(10);
$fields->addFieldToTab('Root.Content', new GridField('ContentSections', 'Content Sections', $this->ContentSections(), $config));
return $fields;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是如何在CMS编辑器选项卡中隐藏/删除不相关的has_one字段?编辑ContentSection时,Person和LandingPage关系下拉字段都会显示,无论是Person还是LandingPage.我只想显示相关的has_one关系字段.我尝试在has_many关系上使用点表示法:
class Person extends DataObject {
protected static $has_many = array(
'ContentSections' => 'ContentSection.Person'
);
}
Run Code Online (Sandbox Code Playgroud)
我也尝试在类的removeFieldFromTab方法中使用该方法,其中我为ContentSection定义了其他CMS字段:getCMSFieldsContentSection
$fields->removeFieldFromTab('Root.Main', 'Person');
Run Code Online (Sandbox Code Playgroud)
而不是removeFieldFromTab使用该removeByName功能.removeFieldFromTab如果没有'Root.Main'标签,则无效.
我们也删除PersonID,而不是Person.has_one变量已ID附加到变量名称的末尾.
function getCMSFields() {
$fields = parent::getCMSFields();
$fields->removeByName('PersonID');
$fields->removeByName('LandingPageID');
return $fields;
}
Run Code Online (Sandbox Code Playgroud)
如果您想有选择地隐藏或显示这些字段,您可以if在getCMSFields函数中添加一些语句.
function getCMSFields() {
$fields = parent::getCMSFields();
if (!$this->PersonID) {
$fields->removeByName('PersonID');
}
if (!$this->LandingPageID) {
$fields->removeByName('LandingPageID');
}
return $fields;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
875 次 |
| 最近记录: |