Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Switching breadcrumbs from different Page Type

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

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

Page: 1
Go to End
Author Topic: 1254 Views
  • Hammy
    Avatar
    Community Member
    49 Posts

    Switching breadcrumbs from different Page Type Link to this post

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

  • Hammy
    Avatar
    Community Member
    49 Posts

    Re: Switching breadcrumbs from different Page Type Link to this post

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

  • Hammy
    Avatar
    Community Member
    49 Posts

    Re: Switching breadcrumbs from different Page Type Link to this post

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

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