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

19 January 2009 at 5:53pm

Sometimes you need to redirect to the first child in your hierarchy, and the following can be put to good use for this:

http://doc.silverstripe.com/doku.php?id=recipes:controllers

But depending on how you customised your hierarchy with allowed_children arrays etc. this can sometimes wreak havoc on your page rendering. I found it easier in some cases to just use it as a function in the Page Controller class (give it a name like function goFirstChild() or something similar), and use the whole thing on a page-by-page basis with a CMS-checkbox-enabled If-block in your template, so you can use it on whatever page without having it load automatically as it does in the original version, if you know what I mean. Hopefully it helps someone, as far as I can see there's no downside to that.

Avatar
TerryMiddleton

Community Member, 108 Posts

21 April 2009 at 10:01am

SilverRay,

This is great information. I'll apologize in advance, as I'm somewhat new, but how do you implement this?

Do you add the function to Page.php? Create a new php file for the page controller you want to affect?

Help,

Terry

Avatar
SilverRay

Community Member, 167 Posts

21 April 2009 at 6:06pm

Edited: 21/04/2009 6:10pm

If you want to have this function on every page you create, put it in the controller section of Page.php -remember, new page types you create inherit from Page.php. So:

<?php

class Page extends SiteTree {

	//This is to make a switch you can use for the function in your template
	public static $db = array(
		'RedirectToChildOne' => 'Boolean'
	);
	
...

	//This will add a checkbox on the CMS side of things to switch on the redirect
	function getCMSFields() {
		$fields = parent::getCMSFields();
		$fields->addFieldToTab( 'Root.Behaviour', new CheckboxField("RedirectToChildOne", "Redirect to first available child of this page, if any."));
		return $fields;
	}

}

class Page_Controller extends ContentController {

...

	function redirectToFirstChild() {
		if($children = $this->Children()) {
			if($firstChild = $children->First()) {
				Director::redirect($firstChild->Link());
			}
		}
	}

...

}

?>

Then in your template, just after the body tag:

<% if Children %>
<% if RedirectToChildOne %>
<% control redirectToFirstChild %>
<% end_control %>
<% end_if %>
<% end_if %>
 ...

(... means whatever other code you might have in your files.)

HTH,
SilverRay.

Avatar
Ben Gribaudo

Community Member, 181 Posts

22 April 2009 at 12:22am

Could you eliminate that template code by putting a call to redirectToFirstChild() in the controller's initialize method?

Ben

Avatar
SilverRay

Community Member, 167 Posts

22 April 2009 at 1:38am

Edited: 22/04/2009 1:40am

Uhm, yes, but then I would lose the ability to switch it on a page by page basis, no? Can you show me what you mean?

Avatar
Ben Gribaudo

Community Member, 181 Posts

25 April 2009 at 3:40am

Edited: 25/04/2009 3:43am

Hi,

Yes, you would loose the ability to control whether or not the page redirected from within template code. However, isn't the purpose of the CMS checkbox to control if the redirect function is on?

As to an example, see below for something I just typed up. Hopefully, it is in (close to) running condition. I'm not sure if the functionality it contains will work in init() or if it must be moved to execute later in the page load process.

Ben

function init() {
	if($children = $this->Children()) {
		if($firstChild = $children->First()) {
			Director::redirect($firstChild->Link());
		}
	}
	return parent::init();
}

Avatar
SilverRay

Community Member, 167 Posts

26 May 2009 at 11:20pm

Hey Ben,

Sorry for the late reply (kinda busy).

In your example above, how would you control that with a checkbox above, then? (in the CMS I mean.)

Avatar
Ben Gribaudo

Community Member, 181 Posts

27 May 2009 at 12:15am

Edited: 27/05/2009 12:16am

You'd add a Boolean $db field, have the field display in the CMS (getCMSFields()), then test whether or not that field is checked when deciding on whether to redirect (pseudo-code: if (checkbox checked && has children) redirect to first child).

Ben

Go to Top