Jump to:

5540 Posts in 1738 Topics by 1224 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Alternative Redirect to First Child use

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

Page: 1 2 3
Go to End
Author Topic: 5872 Views
  • SilverRay
    Avatar
    Community Member
    167 Posts

    Re: Alternative Redirect to First Child use Link to this post

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

  • Ben Gribaudo
    Avatar
    Community Member
    181 Posts

    Re: Alternative Redirect to First Child use Link to this post

    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

  • Juanitou
    Avatar
    Community Member
    323 Posts

    Re: Alternative Redirect to First Child use Link to this post

    Thank you SilverRay! That's a nice idea.

  • SilverRay
    Avatar
    Community Member
    167 Posts

    Re: Alternative Redirect to First Child use Link to this post

    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

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Alternative Redirect to First Child use Link to this post

    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.

  • SilverRay
    Avatar
    Community Member
    167 Posts

    Re: Alternative Redirect to First Child use Link to this post

    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

  • bschmitt
    Avatar
    Community Member
    22 Posts

    Re: Alternative Redirect to First Child use Link to this post

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

  • timwjohn
    Avatar
    Community Member
    98 Posts

    Re: Alternative Redirect to First Child use Link to this post

    Nice banal, very nice.

    5872 Views
Page: 1 2 3
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.