Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Alternative Redirect to First Child use


Go to End
Reply


20 Posts   8615 Views

Avatar
tazzydemon

Community Member, 131 Posts

18 February 2012 at 9:58am

Its easier if you just stick this at the end of your page controller init function rather than use the template



public function init() {
parent::init();

        if($this->RedirectToChildOne && $this->Children()->Count()){
        Director::redirect($this->Children()->First()->AbsoluteLink());

This feature is invaluable if one is using jquerymobile since otherwise the root page is invisible and its content has to be moved to be its own first child

Avatar
NickJacobs

Community Member, 148 Posts

20 March 2012 at 11:14am

Edited: 20/03/2012 11:15am

Hi, just in case anyone wants a variation...I just test to see if the page has any content, then redirect to the first child if not.

if(!$this->dbObject('Content')->hasValue()){ 
    if($this->Children()->Count()){ 
    Director::redirect($this->Children()->First()->AbsoluteLink()); 
    } 
}

Avatar
Friizu

Community Member, 17 Posts

12 December 2015 at 3:27am

As Director::redirect() is deprecated, this is what i used:

public function init() {
   parent::init();
      if($this->Children()->Count()){
      return $this->redirect($this->Children()->First()->Link());
   }
}

Avatar
Friizu

Community Member, 17 Posts

15 December 2015 at 10:38pm

Code i added works but it affects also admin so at the end i solved this issue in template so that's what i added to my menu code in Navigation.ss:

<% if $Children %>
<a href="$Children.First.Link" title="$Title.XML">$MenuTitle.XML</a>
<% else %>
<a href="$Link" title="$Title.XML">$MenuTitle.XML</a>
<% end_if %>

Go to Top