Our Silverstripe project has the following two page types:
class MultiSectionPage extends Page {
private static $allowed_children = array(
'Section'
);
public function PageSections() {
$PageSections = Section::get()->filter(array('ParentID' => $this->ID));
return $PageSections;
}
}
class Section extends Page {
public static $allowed_children = array();
private static $show_in_sitetree = false;
}
In the Layout/MultiSectionPage.ss template the following code loops through each child Section as a dataobject:
<% loop $PageSections %>
<% include MultiSectionPage_Section %>
<% end_loop %>
I want to make sure that if anyone accidentally links to a Section it redirects to the parent MultiSectionPage.
class Section extends Page {
public function Link() {
return parent::Link() . '#section-' . $this->ID;
}
}
class Section_Controller extends Page_Controller {
public function init(){
parent::init();
if(!$this->getResponse()->isFinished() && $link = $this->Link()) {
$this->redirect($link, 301);
return;
}
}
}
However using this method triggers a redirect even when viewing the MultiSectionPage as the init must get called each time the Section DataObject is rendered.
How do I detect whether a Section Controller is being loaded as a standalone parent (redirect) or as child of a MultiSectionPage ?