• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
  • This forum has been archived. New threads and replies may not be made. All add-ons/resources that are active should be migrated to the Resource Manager. See this thread for more information.

[GUIDE] Building Bread Crumbs....

Jaxel

Well-known member
#1
Building Bread Crumbs are a bit different in XF compared to other forums. The changes may seem more complicated, but because of it, its a lot easier to build nested breadcrumbs based on "parents". You can manually create breadcrumbs in templates, such as KingKovifor has figured out, but really there is a better way... I am going through this tutorial using a mod I am currently working on.

First lets start at the template for the page you wish to add breadcrumbs. Instead of hardcoding breadcrumbs, they will instead be dynamically generated using the code below:
Code:
<xen:navigation>
	<xen:breadcrumb source="$breadCrumbs" />
</xen:navigation>

Now lets go to the actual programming. For this mod in particular mod, I created a class to generate these breadcrumbs. Looking at the code below, you can assume this file is located in: "library/EWRporta/Model/Crumbs.php". If you've been programming with Object Oriented Programming the concept of classes should be natural to you.
Code:
<?php

class EWRporta_Model_Crumbs extends XenForo_Model
{
	public function getCrumbs($pageID, &$fullCrumbList = array())
	{
		$db = $this->_getDb()->fetchRow("
			SELECT page_slug, page_name, page_parent
				FROM EWRporta_pages
			WHERE page_id = ?
		", $pageID);

		$fullCrumbList[$db['page_slug']] = array(
			 'value' => $db['page_name'],
			 'href' => 'portal/'.$db['page_slug'],
		);

		if ($db['page_parent'])
		{
			$this->getCrumbs($db['page_parent'], $fullCrumbList);
		}

		return $fullCrumbList;
	}
}
The function "getCrumbs", recieves the ID to the current page, and generates an array of crumbs. It fetches the row information from the database by matching it to the pageID. Then it adds the crumb information to the array. The next part is the function interesting part... The function then checks to see if the particular page has any parent pages (a parent ID of not 0). If it has any parents, the function iterates itself and generates another crumb for the parent, and keeps on iterating itself until it comes to a page without a parent (a parent ID of 0).


Now within the code to the page itself... We need to instantiate the getCrumbs function. After everything we've done so far, this should look simple. We run the function, passing the page ID of the original page, and then reverse the results of the array (otherwise the breadcrumbs will be backwards). After that, we simply pass the $breadCrumbs variable to the $viewParams which you should be passing as the parameters to your page.
Code:
$breadCrumbs = array_reverse($this->getModelFromCache('EWRporta_Model_Crumbs')->getCrumbs($page['page_id']));

$viewParams['breadCrumbs'] = $breadCrumbs;

Remember! I'm an unemployed programmer who enjoys donations!
 

Kier

XenForo Developer
Staff member
#3
You should really use the link builder, or your links are going to break with any URL scheme changes.

Something along these lines - the use of the page_slug should be handed by your Route_Prefix class:
PHP:
 $fullCrumbList[$db['page_slug']] = array(
	'value' => $db['page_name'], 
	 'href' => XenForo_Link::buildPublicLink('full:portal', $db), 
);