2014-12-03 36 views
2

我有一個數據對象稱爲ContentSection有2個has_one關係:對一個頁面類型LandingPage和另一個數據對象PersonSilverstripe刪除不相關的has_one關係字段在CMS選項卡

class ContentSection extends DataObject { 
    protected static $has_one = array(
     'Person'  => 'Person', 
     'LandingPage' => 'LandingPage' 
    ); 
} 

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' 
    ); 
} 

ContentSections通過的LandingPage和人與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; 
} 

我的問題是你怎麼能隱藏/刪除無關HAS_ONE領域的CMS編輯器選項卡?在編輯ContentSection時,無論是Person還是LandingPage,都會顯示Person和LandingPage關係下拉字段。我只想顯示相關的has_one關係字段。我已經使用上的has_many關係點符號的嘗試:

class Person extends DataObject { 
    protected static $has_many = array(
     'ContentSections' => 'ContentSection.Person' 
    ); 
} 

我已經使用在ContentSection類,在這裏我定義其他CMS領域的ContentSection的getCMSFields方法removeFieldFromTab方法也試過:

$fields->removeFieldFromTab('Root.Main', 'Person'); 

回答

7

而不是removeFieldFromTab使用removeByName函數。如果沒有'Root.Main'選項卡,則removeFieldFromTab將不起作用。

此外,我們刪除PersonID,而不是Personhas_one變量已將ID追加到其變量名的末尾。

function getCMSFields() { 
    $fields = parent::getCMSFields(); 

    $fields->removeByName('PersonID'); 
    $fields->removeByName('LandingPageID'); 

    return $fields; 
} 

如果您想選擇隱藏或顯示這些字段,你可以把一些if語句在getCMSFields功能。

function getCMSFields() { 
    $fields = parent::getCMSFields(); 

    if (!$this->PersonID) { 
     $fields->removeByName('PersonID'); 
    } 

    if (!$this->LandingPageID) { 
     $fields->removeByName('LandingPageID'); 
    } 

    return $fields; 
}