Jump to:

5540 Posts in 1738 Topics by 1224 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Page Controls: Next/Previous Page Link?

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

Page: 1 2
Go to End
Author Topic: 6010 Views
  • fabilo
    Avatar
    Community Member
    10 Posts

    Re: Page Controls: Next/Previous Page Link? Link to this post

    cbolt's solution worked well for me. I made two minor edits...

    PreviousPage function would fail if Children return a dataobjectset with no children. So updated the function to use Count():

    function PreviousPage() {
    $where = "ParentID = {$this->ParentID} AND Sort < {$this->Sort}";
    $pages = DataObject::get("SiteTree", $where, "Sort DESC", "", 1);
    if($pages) {
    foreach($pages as $page) {
    // if page has a child go to the last child page
    $children = $page->AllChildren();
    if ($children->Count()) {
    foreach ($children as $child) {
    continue;
    }
    return $child;
    }
    return $page;
    }
    }

    I also updated both functions Where clauses to only return pages shown in the menu. ie:

    $where = "ParentID = $parent AND `SiteTree`.ID != 4 AND Sort > $sort AND ShowInMenus = 1";

  • jaaf
    Avatar
    Community Member
    24 Posts

    Re: Page Controls: Next/Previous Page Link? Link to this post

    Hi,

    previous mechanism seems good here but the last part is missing

        // nothing returned so we jump to the parent page
    if ($this->ParentID > 0) return $this->getParent();

    thus the method should be

    function PreviousPage() {
    $where = "ParentID = {$this->ParentID} AND Sort < {$this->Sort}";
    $pages = DataObject::get("SiteTree", $where, "Sort DESC", "", 1);
    if($pages) {
    foreach($pages as $page) {
    // if page has a child go to the last child page
    $children = $page->AllChildren();
    if ($children->Count()) {
    foreach ($children as $child) {
    continue;
    }
    return $child;
    }
    return $page;
    }
    // nothing returned so we jump to the parent page
    if ($this->ParentID > 0) return $this->getParent();
    }

    It works !

    Moreover it shoud be good to find a mechanism to delete the previous link where at book root and the next link where at book end.

    Added a 3 states BookLoc member to BookPage.php ('Root', 'Middle', 'End')
    and functions like

    function IsNotRoot(){
    return ($this->BookLoc != 'Root');
    }
    function IsNotEnd(){
    return ($this->BookLoc != 'End');
    }

    And used them to validate the matching link in template noticing that control is now deprecated and that you must use with instead. Here is my example :

          <div class="book-both">
             <% if $isNotRoot %>
             <% with PreviousPage %>
                <div class="book-previous" >
                   <a href="$Link" title="Go to the previous page"> &larr; $Title </a>
                </div>
             <% end_with %>

             <% with Parent %>
                <div class="book-toc">
                   <a href="$link" title="Go to mini toc"> Go to one level up </a>
                </div>
             <% end_with %>
             <% end_if %>

             <% if $isNotEnd %>
                <% with NextPage %>
                   <div class="book-next">
                      <a href="$Link" title="Go to the next page">$Title &rarr; </a>
                   </div>
                <% end_with %>
             <% end_if %>
          </div>

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