Faloude Faloude - 4 months ago 22
PHP Question

SilverStripe restrict level-3 child pages

What is the easiest and most convenient way to restrict CMS users from creating level-3 child pages?

I've tried this in

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

}


With this function, I can still create child pages far down the tree, so it doesn't change allowed_children

Answer

You can override SilverStripe's allowedChildren function.

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

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

With this you don't need to set the $allowed_children property.

Comments