Jump to:

3393 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » Show first subpage?

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

Page: 1
Go to End
Author Topic: 1983 Views
  • Romeo
    Avatar
    Community Member
    67 Posts

    Show first subpage? Link to this post

    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?

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Show first subpage? Link to this post

    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());
    }

  • Romeo
    Avatar
    Community Member
    67 Posts

    Re: Show first subpage? Link to this post

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

  • Romeo
    Avatar
    Community Member
    67 Posts

    Re: Show first subpage? Link to this post

    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?

  • Romeo
    Avatar
    Community Member
    67 Posts

    Re: Show first subpage? Link to this post

    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!

  • Pike
    Avatar
    Community Member
    40 Posts

    Re: Show first subpage? Link to this post

    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.......

    1983 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.