Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Customising the CMS /

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

Page Controls: Next/Previous Page Link?


Go to End
Reply


10 Posts   7211 Views

Avatar
fabilo

Community Member, 10 Posts

4 February 2011 at 12:28am

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"; 

Avatar
jaaf

Community Member, 24 Posts

18 February 2014 at 3:03am

Edited: 18/02/2014 8:51pm

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>

Go to Top