Jump to:

5513 Posts in 1732 Topics by 1218 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: 5768 Views
  • SilverRay
    Avatar
    Community Member
    167 Posts

    Alternative Redirect to First Child use Link to this post

    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.

  • TerryMiddleton
    Avatar
    Community Member
    108 Posts

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

    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

  • SilverRay
    Avatar
    Community Member
    167 Posts

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

    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.

  • Ben Gribaudo
    Avatar
    Community Member
    181 Posts

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

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

    Ben

  • SilverRay
    Avatar
    Community Member
    167 Posts

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

    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?

  • Ben Gribaudo
    Avatar
    Community Member
    181 Posts

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

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

  • SilverRay
    Avatar
    Community Member
    167 Posts

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

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

  • Ben Gribaudo
    Avatar
    Community Member
    181 Posts

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

    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

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