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.

Template Questions /

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

Show first subpage?


Go to End


6 Posts   2829 Views

Avatar
Romeo

Community Member, 67 Posts

27 August 2009 at 4:14am

Simple requirement here...I have a News page which contains a number of NewsItem subpages. At the moment, when I go to the News page, I see a submenu displaying all the NewsItem titles. What I would like is to have the first one of these auto-selected and displayed on the page, as if the user has clicked the first item in the submenu. How would I do this?

Avatar
Hamish

Community Member, 712 Posts

27 August 2009 at 8:45am

Edited: 27/08/2009 8:46am

You could add a redirection to the index (default) method of news page:

function index() {
	// untested
	Director::redirect($this->Children()->First()->URLSegment);
}

Preferably though, you could push the response of that page instead (and avoid the redirect). Something like:

function index() {
	// untested
	$page = $this->Children()->First();
	return $this
		->customise('Title' => $page->Title, 'Content' => $page->Content)
		->renderWith(array());
}

Avatar
Romeo

Community Member, 67 Posts

29 August 2009 at 3:08am

Great, thanks for the suggestions, I'll give them a go and report back.

Avatar
Romeo

Community Member, 67 Posts

29 August 2009 at 3:46am

Hmm, not quite there. The second method qives me the following error:

Parse error: syntax error, unexpected T_DOUBLE_ARROW in /var/www/fsbi/mysite/code/SubpageHolder.php on line 15

SubpageHolder is the subclass of Page I used for this purpose.

The first method looks as if it should work but for the fact that it redirects to a subpage with the wrong URL, but this brings up another issue. Let's say my page is /news/ and I want it automatically directed to the first news article. Your first method is sending me to

/news/article1

Fair enough, that's probably how it should be, but the actual URL is /article1 (i.e., without the prefacing /news). Why this should be the case, I don't know, because article1 has been created in the CMS as a subpage of the news page. I would have thought the URL would be indicative of this, but it isn't. Is there a way to make it so?

Avatar
Romeo

Community Member, 67 Posts

29 August 2009 at 4:23am

The answer is obvious to me now:

function index() {
// untested
$url=Director::baseURL().$this->Children()->First()->URLSegment;
Director::redirect($url);
}

Having done a bit of research I see that the question of nested URLs has been going on a long time now. Looking forward to 2.4!

Avatar
Pike

Community Member, 42 Posts

3 January 2010 at 2:34am

Hi,

I don't know if it's good writen, but.

I want: if first menuitem Content is empty get Content of next menuitem (if exist), so solving for me was that code (overriding iindex() method in Page_Controller)

function index() {
if ( $this->ClassName == 'Page' &&
( empty( $this->Content ) || strlen( $this->Content ) < 6 ) && ( $children = $this->Children() )
) {
$url = Director::baseURL() . $this->Children()->First()->URLSegment;
Director::redirect($url);
}
else {
return $this->renderWith( array( $this->ClassName, 'Page' ) );
}
}

I hope that it helps to others.......