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?
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.
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());
}
Great, thanks for the suggestions, I'll give them a go and report back.
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?
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!
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.......