2016-08-04 55 views
2

什麼是限制CMS用戶創建3級子頁面的最簡單方便的方法?SilverStripe限制3級子頁面

我在class Page

public function canHaveChild() { 

    //Get SiteTree column value ParentID of this record 
    $parentID = DataObject::get("SiteTree", "WHERE ID = '$this->ID'")->ParentID; 

    //If parentID = 0, this is a root page, so it can have a childpage 
    if($parentID == 0) { 
     $this->allowed_children = array("Page", "BasicPage", "FormPage"); 
    } else { 
     $this->allowed_children = false; 
    } 

} 

使用此功能試過,我仍然可以創建子頁面遠了樹,所以它不會改變allowed_children

回答

4

您可以覆蓋SilverStripe的allowedChildren功能。

class Page extends SiteTree 
{ 
    public function allowedChildren() 
    { 
     if($this->Level(3)) 
      return []; 

     return ['Page', 'BasicPage', 'FormPage']; 
    } 
} 

有了這個,你不需要設置$allowed_children屬性。

+0

完美使用'$ this-> Level(2)' – Faloude