Semicolon
Semicolon

Reputation: 1914

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

Upvotes: 2

Views: 93

Answers (1)

Fatal Error
Fatal Error

Reputation: 1024

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.

Upvotes: 4

Related Questions