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.

Customising the CMS /

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

Alternative Redirect to First Child use


Go to End


20 Posts   11985 Views

Avatar
SilverRay

Community Member, 167 Posts

27 May 2009 at 12:39am

You mean, you put the above pseudo code in the Page class instead of the HTML like I did?

Avatar
Ben Gribaudo

Community Member, 181 Posts

27 May 2009 at 7:44am

You mean, you put the above pseudo code in the Page class instead of the HTML like I did?
Yes, in a page class.

Ben

Avatar
Juanitou

Community Member, 323 Posts

22 July 2009 at 9:14pm

Thank you SilverRay! That's a nice idea.

Avatar
SilverRay

Community Member, 167 Posts

1 February 2010 at 6:59pm

Hey Ben,

Just coincidentally I stumbled on this thread again and noticed that I never followed up, so here it goes, just in case other users find it:

class Page_Controller extends ContentController {
	public function init() {
		if($this->RedirectToChildOne) {
			if($children = $this->Children()) {
				if($firstChild = $children->First()) {
					Director::redirect($firstChild->Link());
				}
			}
		}
	parent::init();
	}
}

In this way you can forego the template code. Thanks Ben for reminding me on this method ;)

SR

Avatar
bummzack

Community Member, 904 Posts

1 February 2010 at 8:28pm

Let me suggest an improvement: Override the Link method of your class with a code like this:

function Link() {
	if($this->RedirectToChildOne) { 
		if($this->Children()->First())
			return $this->Children()->First()->Link();
	}
	return parent::Link();
}

That way, the page-link will point to the first child if RedirectToChildOne is checked, saving the user (and the webserver) from a lot of redirects.

For first-child redirects, I usually use a specialized page class (http://pastie.org/803850). I don't see the point in having a checkbox, since you could just as well change the page-type in the Behavior tab. This is preferable imho, since it updates the CMS UI accordingly. You no longer have a content field to enter content that will never be shown and you can use another page-icon that helps the user to easily spot these redirector pages.

Avatar
SilverRay

Community Member, 167 Posts

1 February 2010 at 8:51pm

Ah, very smart banal, thanks for your suggestion! There can be a lot of redirects indeed when multiple pages in a hierarchy have redirects.

I used to have a separate class for redirects actually, then I thought to bring it to the Page class for universal use, but your approach makes sense. In my case, sometimes a user wants to revert back to the original page without a redirect, that's why I made it a checkbox, the original page with contents is then still there...

Anyway, I should think more of overriding existing methods ;)

SR

Avatar
bschmitt

Community Member, 22 Posts

1 May 2010 at 12:30am

@banal: Many thanks for sharing, that was exactly what I was looking for :-) Saved me a lot if time!
Best Bjoern

Avatar
timwjohn

Community Member, 98 Posts

1 December 2010 at 6:08am

Nice banal, very nice.