Joomla 1.5 Custom Breadcrumbs

Posted by: gbluma

Tagged in: Programming , PHP , Joomla , Components

Joomla seems to have everything figured out — Breadcrumb support is no exception.

Recently we were writing some custom component for a client and needed to handle the breadcrumbs in a special way that Joomla couldn't manage automatically.

As a simplified example, imagine we redirect a user to a component that isn't attached to a menu. We would expect the breadcrumbs to simply output "Home" and provide a link back to the index of the site.

Home 

(Notice: Home is not a link while on homepage)

But let's try to make this a little more usable. In our example, let's say that we've landed on a Product Index page. If we use the code:

global $mainframe;
$app =& JFactory::getApplication();
$pathway =& $app->getPathway(); // get the pathway object we want to modify
$pathway->addItem('Products');  // add the text, but no link

So If we're on the product index of our component, we would see this as our breadcrumb:

Home > Products

(Notice: "Home" is linked, "Products" is not)

Adding text is nice, but let's up the ante. Let's also say in our example, that the user clicks a specific product on our product index ("The Breadslic0r 3000"). As we go to that page, we expect products to be clickable and for "The Breadslic0r 3000" to show up in the breadcrumbs as the current item.

Let's see the code for it:

global $mainframe;
$app =& JFactory::getApplication();
$pathway =& $app->getPathway(); // get the pathway object we want to modify
$pathway->addItem('Products', 'index.php?option=com_products&task=products');  // add the text, this time w/ link
$pathway->addItem('The Breadslic0r 3000'); // add the text, w/ no link

(Notice: this is a generic example, in a production environment I'd expect the product name to be added dynamically)

And now let's see the output:

Home > Products > The Breadslic0r 3000

(Note: "Home" and "Products" are both linked, "The Breadslic0r 3000" is not)

It's not the most amazing thing ever, but it might be useful to know. Let us know if this has been useful for you!