Question

I have two custom page types in Silverstripe called TeamPage and TeamReport.

When a new TeamPage is created I want TeamReport added as a single child page - with '/report/' as the URL slug.

What code should I add to TeamPage for this to happen?

Was it helpful?

Solution

For this, I would create the TeamReport in an onAfterWrite method on TeamPage, after checking for creation in onBeforeWrite.

public function onBeforeWrite() {
    parent::onBeforeWrite();
    $this->IsCreating = !$this->ID;
}

public function onAfterWrite() {
    parent::onAfterWrite();
    if ($this->IsCreating) {
        $child = new TeamReport();
        $child->ParentID = $this->ID;
        $child->URLSegment = 'report';
        $child->Title = 'Report';
        $child->write();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top