Jump to:

23368 Posts in 18143 Topics by 2863 members

General Questions

SilverStripe Forums » General Questions » Page Controller - Query if page has a certain parent

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 1645 Views
  • njprrogers
    Avatar
    Community Member
    23 Posts

    Page Controller - Query if page has a certain parent Link to this post

    Hi,

    I have my page_controller and I wish to find out whether the current page is in a particular section (similar to InSection in the templating layer!).

    Can anyone advise how I would go about this?

    Thanks!

    Nick

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: Page Controller - Query if page has a certain parent Link to this post

    Anything that you see in the template is really a function within the controller...

    in this case the SiteTree controller so in any page you can type

    if ($this->InSection('xyz')) {
    //do stuff
    }

  • Ben_W
    Avatar
    Community Member
    80 Posts

    Re: Page Controller - Query if page has a certain parent Link to this post

    I usually create one page type for each root level page, or key page, most of time place holder pages, and limit the occurrence of these page to one instance only by adding the following.

    class ApplicationPageHolder extends Page {
    ...

    //only allow one instance
       public function canCreate() {
          return !DataObject::get_one("SiteTree", "ClassName ='ApplicationPageHolder'");
       }
    ...

    }

    Example of site tree:

    ApplicationPageHolder
    - ApplicationPage
    - ApplicationPage
    - ApplicationPage
    GalleryPageHolder
    - ImageGalleryPage
    - VideoGalleryPage

    In the Page.php Controller, you may add these functions.

    class Page_Controller extends ContentController {
    ...

    /**
        * get root parent page
        */
       function FinalParent($page = null) {
          if(!$page){
             $page = $this;
          }
          if($page->ParentID > 0) {
             return $this->FinalParent($page->Parent());
          }
          else {
             return $page;
          }
       }

    function FinalParentClassName(){
          //get root parent page id
          $parent_page = $this->FinalParent();
          $parent_page_class_name = $parent_page->ClassName;
          
          return $parent_page_class_name;
       }
    ...

    }

    Now in the ApplicationPage.ss you can do

    <% if FinalParentClassName = ApplicationPageHolder%>
    display something
    <% end_if %>

    You may also create your own function in Page_Controller to do some specific check you need, just use FinalParent() which always return the root level page. The advantage of doing this (creating individual page type for each root level page) is that URLSegment can be changed due to user update, whereas ClassName will always stay as it is.

    Hope this helps.

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