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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Redirect page to first child page


Go to End


2 Posts   1044 Views

Avatar
purtle

Community Member, 11 Posts

28 September 2016 at 5:18pm

Hi there

I'm upgrading a 2.4 site to 3.4

I currently have the following code to check if page content exists and, if not, redirect to the first child page:

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

I understand that Director::redirect() is deprecated so have replaced that line with:

return $this->redirect($this->Children()->First()->AbsoluteLink());

This works to redirect the page, but if I have content in the parent, it still redirects to the first child.

I'm assuming there is something wrong with the first line of code above that needs to be updated too?

Many thanks
Grant

Avatar
ZarockNZ

Community Member, 17 Posts

17 October 2016 at 4:01pm

Hi,

Surely something a simple as this would work...

if (empty($this->Content)) {
    // redirect.
}