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.

Archive

Our old forums are still available as a read-only archive.

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

Switching breadcrumbs from different Page Type


Reply

3 Posts   1300 Views

Avatar
Hammy

5 November 2008 at 10:00pm Community Member, 49 Posts

I'm a bit of a novice at this but here goes.

I have two page types - CategoryPage and ProductPage, where a ProductPage has a single relationship to a CategoryPage type.

Instead of displaying the breadcrumb from where the ProductPage is within the SiteTree, I want to use the CategoryPage that is related to the ProductPage and use this in breadcrumb.

So instead of Home > ProductHolder > ProductPage, it should instead display Home > CategoryPage > CategoryPage > ProductPage.

I have created (well actually copied and modified from SiteTree.php) the following code where the main changes has been at the end of the while loop where it checks the page type. Its not breaking but its not showing the category path back to the home page either (ie it only shows Home > ProductPage).

public function LoadedBreadcrumbs($maxDepth = 20, $unlinked = false,
                                             $stopAtPageType = false) {
      $parts = array();
      $i = 0;
      $page = $this;      
      
      while(($page && (sizeof($parts) < $maxDepth))   ||
               ($stopAtPageType && $page->ClassName != $stopAtPageType)) {
         if($page->ShowInMenus || ($page->ID == $this->ID)) {
            if($page->URLSegment == 'home') {
               $hasHome = true;
            }
            $parts[] = (($page->ID == $this->ID) || $unlinked)
               ? "<li>" . Convert::raw2xml($page->Title) . "</li>"
               : ("<li><a href=\"" . $page->Link() . "\">" . Convert::raw2xml($page->Title) . "</a> &gt; </li>");
         }
         
         if($page->ClassName == "ProductPage"){
            $category = $page->CategoryID;
            $filter = "`SiteTree_Live`.ID = $category";
            $page = DataObject::get('CategoryPage',$filter);
         } else {
            $page = $page->Parent;
         }
      }

      return implode(self::$breadcrumbs_delimiter, array_reverse($parts));
   }

Can anyone help with switching from one page type to another related page type in breadcrumb? Thanks in advance....

Avatar
Hammy

5 November 2008 at 10:04pm Community Member, 49 Posts

WOOOPS - sorry, wrong snippet of code... Doh

Here is the code that is currently in use

public function LoadedBreadcrumbs($maxDepth = 20, $unlinked = false,
                                             $stopAtPageType = false) {
      $parts = array();
      $i = 0;
      $page = $this;      
      
      while(($page && (sizeof($parts) < $maxDepth))   ||
               ($stopAtPageType && $page->ClassName != $stopAtPageType)) {
         if($page->ShowInMenus || ($page->ID == $this->ID)) {
            if($page->URLSegment == 'home') {
               $hasHome = true;
            }
            $parts[] = (($page->ID == $this->ID) || $unlinked)
               ? "<li>" . Convert::raw2xml($page->Title) . "</li>"
               : ("<li><a href=\"" . $page->Link() . "\">" . Convert::raw2xml($page->Title) . "</a> &gt; </li>");
         }
         
         if($page->ClassName == "ProductPage"){
            $category = $page->CategoryID;
            $filter = "`CategoryPage`.ID = $category";
            $page = CategoryPage::get('CategoryPage',$filter,'Title DESC');
         } else {
            $page = $page->Parent;
         }
      }

      return implode(self::$breadcrumbs_delimiter, array_reverse($parts));
   }

Thanks again in advance...

Avatar
Hammy

6 November 2008 at 9:38pm Community Member, 49 Posts

After some trial an error - finally got it working... Just incase anyone else has a similar issue, here is the code that worked for me.

public function Breadcrumbs($maxDepth = 20, $unlinked = false, $stopAtPageType = false) {
      $parts = array();
      $i = 0;
      $page = $this;      
      while(($page && (sizeof($parts) < $maxDepth))   ||
               ($stopAtPageType && $page->ClassName != $stopAtPageType)) {
         if($page->ShowInMenus || ($page->ID == $this->ID)) {
            if($page->URLSegment == 'home') {
               $hasHome = true;
            }
            $parts[] = (($page->ID == $this->ID) || $unlinked)
               ? "<li>" . Convert::raw2xml($page->Title) . "</li>"
               : ("<li><a href=\"" . $page->Link() . "\">" . Convert::raw2xml($page->Title) . "</a> &gt; </li>");
         }
         if($page->ClassName == "ProductPage"){
            $category = $page->CategoryID;
            $filter = "`CategoryPage`.ID = $category";
            $page = CategoryPage::get_one('CategoryPage',$filter,'Title DESC');
         } else {
            $page = $page->Parent;
         }
      }

      return implode(self::$breadcrumbs_delimiter, array_reverse($parts));
   }

This should allow you to change the breadcrumb based on a single relationship with another page type that is in the site tree.

Hope this is of help to someone...