HTML Logo by World Wide Web Consortium (www.w3.org). Click to learn more about our commitment to accessibility and standards.

Moving forward with Composr

ocPortal has been relaunched as Composr CMS, which is now in beta. ocPortal 9 will be superseded by Composr 10.

Head over to compo.sr for our new site, and to our migration roadmap. Existing ocPortal member accounts have been mirrored.


Breadcrumbs

There are no pages beneath this page

Posted
Submitted by Chris Graham
If you are making a new module with tree-based categories, and want to show breadcrumbs…

Let's say you have a front page to the module, and you have something in a category with a parent category "Bar" and that has a parent category "Foo" and that has a parent category "Root".

This would create: Module index > Root > Foo > Bar

PHP code

breadcrumb_set_parents(array(
    array(
'_SELF:_SELF:misc','Module index'), // type=misc
    
array('_SELF:_SELF:category:1','Root'), // type=category&id=1
    
array('_SELF:_SELF:category:3','Foo'), // type=category&id=3
    
array('_SELF:_SELF:category:6','Bar'), // type=category&id=6
));


_SELF:_SELF means "current zone, current page". It stops you having to hard-code zone and page names.

ocPortal automatically puts stuff in front, and puts the title of the current page on the end (as a non-link), so it would show something like: Home > Module index > Root > Foo > Bar > Something

Of course, in real code you are more likely to put this together in a loop, so your code will be more like:

PHP code

$breadcrumbs=array();
$_category_id=$category_id;
do 
// Recursively find parents and put each as a breadcrumb
{
    list(
$_category_id,$_category_name)=find_parent($_category_id);
    if (
$_category_id!==NULL)
    {
        
$breadcrumbs[]=array('_SELF:_SELF:category:'.strval($_category_id),$_category_name);
    }
}
while (
$_category_id!==NULL);
$breadcrumbs[]=array('_SELF:_SELF:misc','Module index');
breadcrumb_set_parents(array_reverse($breadcrumbs));


The above code is just illustrative. The find_parent function is not real, nor is $category_id – substitute as appropriate for whatever you write/use yourself.

If the title of the current page is too long or looks weird in the breadcrumbs, you can override it

PHP code

breadcrumb_set_self('Thing');

 
CEDI change-log Post