We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29494
    • 3 Posts
    I wrote an extender which outputs a breadcrumbs navigation in Ditto.

    Save the code below as "/assets/snippets/ditto/extenders/breadcrumbs.extender.inc.php" and include it in your ditto call with "&extenders=`breadcrumbs`".
    Place a placeholder [+breadcrumbs+] in your custom template and you are done.

    The breadcrumbs output itself can be completely customized with templates. See source code for more information.

    I wrote this for myself, but maybe you find it useful...

    Regards, Tensai

    <?php
    
    /*
     * Title: Breadcrumbs
     * Purpose:
     *  	Legacy support for the [+breadcrumbs+] placeholder
    */
    
    $placeholders['breadcrumbs'] = array("breadcrumbs","bcDetermineBreadcrumbs","");
    
    $bcStartID = isset($bcStartID) ? $bcStartID : $parents;
     /*
    	Param: bcStartID
    
    	Purpose:
    	ID of the start site for the breadcrumbs navigation.
    	If set to '0', the breadcrumbs naviagtion will start from the root.
    	If not set, breadcrumbs navigation will start from the parent site as defiend in the ditto call with &parents=``.
    	If more than one parents site are defined, navigation will start from the root.
    
    	Options:
    	A valid document ID.
    	
    	Default:
    	$parents
    */
    
    $bcRootName = isset($bcRootName) ? $bcRootName : 'Home';
     /*
    	Param: bcRootName
    
    	Purpose:
    	Name to be shown for the root folder
    
    	Options:
    	A valid string.
    
    	Default:
    	"Home"
    */
    
    $bcTpl = isset($bcTpl)? template::fetch($bcTpl) : '<a href="[+bcLink+]"><i>[+bcName+]</i></a> <span style="color:#FF7400;">»</span> ';
    /*
    	Param: bcTpl
    
    	Purpose:
    	Template for the crumbs of the breadcrumbs.
    	Placeholders to be used are:
    	[+bcID+] : ID of the folder/site. Is empty if root folder.
    	[+bcLink+] : Link to the folder/site. Can be used as href="[+bcLink+]".
    	[+bcName+] : Name of the folder/site (=pagetitle).
    
    	Options:
    	A valid chunk name
    
    	Default:
    	"<a href="[+bcLink+]"><i>[+bcName+]</i></a> <span style="color:#FF7400;">»</span> "
    */
    
    $bcTplFirst = isset($bcTplFirst)? template::fetch($bcTplFirst) : $bcTpl;
    
    /*
    	Param: bcTplFirst
    
    	Purpose:
    	Template for the first crumb of the breadcrumbs.
    	Placeholders to be used are:
    	[+bcID+] : ID of the folder/site. Is empty if root folder.
    	[+bcLink+] : Link to the folder/site. Can be used as href="[+bcLink+]".
    	[+bcName+] : Name of the site (=pagetitle).
    	
    	Options:
    	A valid chunk name
    
    	Default:
    	$bcTpl
    */
    
    $bcTplLast = isset($bcTplLast)? template::fetch($bcTplLast) : '<a href="[+bcLink+]"><b>[+bcName+]</b></a>';
    /*
    	Param: bcTplLast
    
    	Purpose:
    	Template for the last crumb of the breadcrumbs.
    	Placeholders to be used are:
    	[+bcID+] : ID of the folder/site. Is empty if root folder.
    	[+bcLink+] : Link to the folder/site. Can be used as href="[+bcLink+]".
    	[+bcName+] : Name of the site (=pagetitle).
    	
    	Options:
    	A valid chunk name
    
    	Default:
    	"<a href="[+bcLink+]"><b>[+bcName+]</b></a>"
    */
    
    $GLOBALS['ditto_breadcrumbs_params'] = compact('bcStartID','bcRootName','bcTpl','bcTplFirst','bcTplLast');
    
    // ---------------------------------------------------
    // Breadcrumbs Functions
    // ---------------------------------------------------
    
    if (!function_exists("bcDetermineBreadcrumbs")) {
    	function bcDetermineBreadcrumbs($resource) {
    		global $modx;
    		global $ditto_breadcrumbs_params;
    		$id = $resource['id'];
    		$link = '[~'.$id.'~]';
    		$name = $resource['pagetitle'];
    		$newcrumb = $ditto_breadcrumbs_params['bcTplLast'];
    		$newcrumb = str_replace('[+bcID+]',$id,$newcrumb);
    		$newcrumb = str_replace('[+bcLink+]',$link,$newcrumb);
    		$newcrumb = str_replace('[+bcName+]',$name,$newcrumb);
    		$output = $newcrumb;
    		do {
    			$parentID = bcGetDocuments(array($id),1,0,'parent');
    			$parentTitle = bcGetDocuments($parentID[0],1,0,'pagetitle');
    			$id = isset($parentID[0]['parent'])? $parentID[0]['parent'] : '0';
    			if (($id==$ditto_breadcrumbs_params['bcStartID']) OR ($id=='0')){
    				if ($id=='0'){unset($id);}
    				$link = isset($id)? '[~'.$id.'~]' : '/';
    				$name = isset($parentTitle[0]['pagetitle'])? $parentTitle[0]['pagetitle'] : $ditto_breadcrumbs_params['bcRootName'];
    				$newcrumb = $ditto_breadcrumbs_params['bcTplFirst'];
    				$newcrumb = str_replace('[+bcID+]',$id,$newcrumb);
    				$newcrumb = str_replace('[+bcLink+]',$link,$newcrumb);
    				$newcrumb = str_replace('[+bcName+]',$name,$newcrumb);
    				$output = $newcrumb . $output;
    				break;
    			}
    			$link = '[~'.$id.'~]';
    			$name = $parentTitle[0]['pagetitle'];
    			$newcrumb = $ditto_breadcrumbs_params['bcTpl'];
    			$newcrumb = str_replace('[+bcID+]',$id,$newcrumb);
    			$newcrumb = str_replace('[+bcLink+]',$link,$newcrumb);
    			$newcrumb = str_replace('[+bcName+]',$name,$newcrumb);
    			$output = $newcrumb . $output;
    		} while (1);
    		return $output;
    	}
    }
    
    if (!function_exists("bcGetDocuments")) {
    	function bcGetDocuments($ids= array (), $published= 1, $deleted= 0, $fields= "*", $where= '', $sort= "menuindex", $dir= "ASC", $limit= "") {
    		global $modx;
    		if (count($ids) == 0) {
    			return false;
            }
    		else {
    			$limit= ($limit != "") ? "LIMIT $limit" : ""; // LIMIT capabilities - rad14701
                $tblsc= $modx->getFullTableName("site_content");
                $tbldg= $modx->getFullTableName("document_groups");
                // modify field names to use sc. table reference
                $fields= 'sc.' . implode(',sc.', preg_replace("/^\s/i", "", explode(',', $fields)));
                $sort= ($sort == "") ? "" : 'sc.' . implode(',sc.', preg_replace("/^\s/i", "", explode(',', $sort)));
                if ($where != '')
                    $where= 'AND ' . $where;
                // get document groups for current user
                if ($docgrp= $modx->getUserDocGroups())
                    $docgrp= implode(",", $docgrp);
                $sql= "SELECT DISTINCT $fields FROM $tblsc sc
                        LEFT JOIN $tbldg dg on dg.document = sc.id
                        WHERE (sc.id IN (" . join($ids, ",") . ") AND sc.published=$published AND sc.deleted=$deleted $where)
                       GROUP BY sc.id " .
                 ($sort ? " ORDER BY $sort $dir" : "") . " $limit ";
                $result= $modx->dbQuery($sql);
                $resourceArray= array ();
                for ($i= 0; $i < @ $modx->recordCount($result); $i++) {
                    array_push($resourceArray, @ $modx->fetchRow($result));
                }
                return $resourceArray;
            }
        }
    }
    ?>
    
      • 33337
      • 3,975 Posts
      Tensai,

      First post with awesome contribution! laugh Man! we need more guys like you.

      Thanks a lot for this. smiley

      Zaigham
        Zaigham R - MODX Professional | Skype | Email | Twitter

        Digging the interwebs for #MODX gems and bringing it to you. modx.link
        • 20413
        • 2,877 Posts
        THANK YOU TENSAI!! cool
          @hawproductions | http://mrhaw.com/

          Infograph: MODX Advanced Install in 7 steps:
          http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

          Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
          http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
          • 7231
          • 4,205 Posts
          Great contribution and a great example of how to use Ditto extenders grin
            [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

            Something is happening here, but you don&#39;t know what it is.
            Do you, Mr. Jones? - [bob dylan]
            • 29494
            • 3 Posts
            Thanks a lot for your kind replies.

            However, please do not expect too much.
            I was not able to test it in every detail and the code is maybe be written very amateurish because I am not a professional programmer.
            As I said, I wrote it basically for myself because I just needed such functionality for my private homepage.
            For me it is working but don’t expect it to work for you as well...

            And I will not give support for this piece of code as I will certainly not have time for this.

            Regards, Tensai
              • 33337
              • 3,975 Posts
              Tensai,

              No problem. Atleast you shared back your code with the community. smiley

              Best regards.
                Zaigham R - MODX Professional | Skype | Email | Twitter

                Digging the interwebs for #MODX gems and bringing it to you. modx.link