[Guide] Building Bread Crumbs....

Status
Not open for further replies.

Jaxel

OMG Member
Joined
Oct 6, 2010
Messages
24
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!
 

Floris

I'm just me :) Hi.
Staff member
Joined
Jan 1, 2001
Messages
60,096
King made a great tutorial for those who are new to this and aren't making custom pages. You've gone level up and made it ready for the developers! Thumbsup.
 
Status
Not open for further replies.
Top