문제

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?

도움이 되었습니까?

해결책

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();
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top