Breadcrumb

Permalink
Any suggestions as to the best way to show a breadcrumb to let users know where they are within the navigation. Preferably in a format that looks something like this:

Top Page > Sub Page > Sub-sub page

jgarcia
 
jgarcia replied on at Permalink Reply
jgarcia
Here's what I came up with...

<?php
$nh = new NavigationHelper();
$breadcrumb = $nh->getTrailToCollection($c);
$bc_count = 0;
foreach ($breadcrumb as $bcpage) {
   //Don't put the separator if its the first item
   if ($bc_count != 0) {
      echo ' &gt; ';
   }
   echo $bcpage->getCollectionName();
   $bc_count++;
}
?>
jgarcia replied on at Permalink Reply
jgarcia
Couple of notes...the array actually has to be sorted using krsort in order to display the pages in the proper order (top first).

Also, this doesn't display the current page, so code needs to be added for that. Here's the adjusted code

<?php
$nh = new NavigationHelper();
$breadcrumb = $nh->getTrailToCollection($c);
krsort($breadcrumb);
foreach ($breadcrumb as $bcpage) {
   echo $bcpage->getCollectionName() . ' &gt; ';
}
echo $c->getCollectionName();
?>
jgarcia replied on at Permalink Reply
jgarcia
For some reason the code printed above works in FF but not in IE or Google Chrome. I'm getting this error:

Fatal error: Class 'NavigationHelper' not found in /srv/www/htdocs/concrete/themes/mc/one_column.php on line 211

Any thoughts?
jgarcia replied on at Permalink Reply
jgarcia
Forgive me again. I was logged in in Firefox but not IE...that was why it worked in FF. Is there a way to get this code working for a non-authenticated user?
smilkobuta replied on at Permalink Reply
replace jgarcia's code, "$nh = new NavigationHelper(); " -> "$nh = Loader::helper('navigation');".

it work whenever user is login or not.
here is full code.

function show_breadcrumb($c) {
        $nh = Loader::helper('navigation');
        $breadcrumb = $nh->getTrailToCollection($c); 
        krsort($breadcrumb); 
        foreach ($breadcrumb as $bcpage) { 
            echo '<a href="' . $bcpage->getCollectionPath() . '/">' . $bcpage->getCollectionName() . '</a> &gt; '; 
        } 
        echo $c->getCollectionName(); 
    }
    // output
    show_breadcrumb($c);
Styves replied on at Permalink Reply
Styves
Would it be possible to add this feature in the next version of C5?

Because I really can't not do what your talking about. I'm surely not the only one, no?

Thanks.
rbn replied on at Permalink Reply
I've just had a play with this. Combining the work of smilkobuta (post above) and the text below from the pagehttp://www.concrete5.org/help/documentation/developers/understandin...

I came up with the file breadcrumbs.php

The documentation page says:
Let's say you want to create a new template for your autonav block. The built-in autonav template is mostly fine, but you want a "breadcrumb" template as well. In your local blocks/ directory (which is typically empty when you install Concrete5), create

autonav/templates/breadcrumb.php

Paste the code from concrete/blocks/autonav/view.php into this file, and then modify as necessary. Then, when you wish to use this template, click on the block you'd like to modify, and select "Choose Custom Template" from the menu. Your "Breadcrumb" template should appear in the select menu.

The file breadcrumbs.php modified from the view.php file which I used was:
<?php 
   defined('C5_EXECUTE') or die(_("Access Denied."));
   $aBlocks = $controller->generateNav();
   global $c;
   //output
show_breadcrumb($c);   
function show_breadcrumb($c){ 
$nh=Loader::helper('navigation'); 
$breadcrumb=$nh->getTrailToCollection($c); 
krsort($breadcrumb); 
foreach($breadcrumb as $bcpage){ 
echo'<a href="'.BASE_URL.DIR_REL.$bcpage->getCollectionPath().'/">'.$bcpage->getCollectionName().'</a>&gt;'; 
} 
echo$c->getCollectionName(); 
}

This uses the function that smilkobuta detailed with a minor modification: I added BASE_URL.DIR_REL to the link which is created, so that it worked in my install which is in a sub directory. Also, for it to work properly you need to have pretty_urls selected in the sitewide settings. Otherwise, you have to include index.php also in the url which is generated in the breadcrumbs file for it to work.

Finally, to use it, you install an autonav block, and then click on the installed block and choose the custom template file breadcrumbs.php (which you have just installed) to activate it.

Hope this makes it a bit clearer how you can implement a breadcrumb autonav.
chassa2556 replied on at Permalink Reply
chassa2556
My breadcrumb is viewing but its not showing the second level items as it should. I have a top level header nav area with my parent items - when you click on News/Events it comes up with a secondary breadcrumb underneath with News item 1, 2 and 3 etc.

Although in the preview its showing news item 1, news item 2, news item 3 when I change the template to breadcrumb it defaults back to the top level home > news/events in the breadcrumb. These are parent levels.

It seems to order fine when I use the header nav template but I want to use the breadcrumb template. What am I doing wrong any ideas??
Slacker replied on at Permalink Reply
Slacker
I just wanted to say thanks for sharing the code.

I actually put this on my template and shows the breadcrumb nicely without having to add any extra blocks. =)
melat0nin replied on at Permalink Reply
melat0nin
This code doesn't work when comparing versions - I get the following error when I try:

Fatal error: Cannot redeclare show_breadcrumb() (previously declared in /c5/themes/SLC/default.php:72) in /c5/themes/SLC/default.php  on line 80


Any thoughts?
Dag replied on at Permalink Reply
Is it possible to modify this so that the breadcrumb list does not include the top level/menu item of the site?

I have this main navigation:

- Home
- Technologies
* Technologies subpage 1
* Technologies subpage 2
* Technologies subpage 3
- Mainmenuitem 3
- Mainmenuitem 4

Currently the crumbs displays as:

Home / Technologies / Technologies subpage 1 / Technologies subpage 2

I want it to just be:
Technologies / Technologies subpage 1 / Technologies subpage

I'm really only skilled in html and css, php is new ground for me, tried a couple of things that resulted in fatal errors.

Any help would be greatly appreciated.
jordanlev replied on at Permalink Reply
jordanlev
How are you showing the breadcrumbs? Is it with the autonav block (using the "breadcrumb" custom template)? If so, you can customize the breadcrumb template by doing the following:

* Create a new directory on your server at SITEROOT/blocks/autonav/templates

* Copy the file SITEROOT/concrete/blocks/autonav/templates/breadcrumb.php to the new directory you created above, so the new copy is SITEROOT/blocks/autonav/templates/breadcrumb.php

* Edit the new copy of the file, find this line:
$_c = $ni->getCollectionObject();

...and directly below it, add this new line:
if ($_c->getCollectionID() == HOME_CID) { continue; } //skip home page
Dag replied on at Permalink Reply
Thanks heaps! This worked like a charm.

I used it on the breadcrumb.php file that came installed with concrete5 though, and not the one listed as code further up, that one didn't work.
jinglesthula replied on at Permalink Reply
jinglesthula
I have the breadcrumbs working just fine, but I'd like to add a visual separator between the links. It would be nice to have the option of specifying either a character or an image to use as the separator. Thus:

link > link > link

Right now I just have it thus:

link link link
rbn replied on at Permalink Reply 2 Attachments
Well you can change it quite easily in the code
echo'<a href="'.BASE_URL.DIR_REL.$bcpage->getCollectionPath().'/">'.$bcpage->getCollectionName().'</a>&gt;';
contains the separator (the &gt;)
You could for example use a - by using - here instead of the &gt;
An image could be used used by changing this line to:
echo'<a href="'.BASE_URL.DIR_REL.$bcpage->getCollectionPath().'/">'.$bcpage->getCollectionName().'</a><img src="'.BASE_URL.DIR_REL.'/blocks/autonav/templates/myseparator.jpg" alt="separator" />';

and including a suitable graphic file (like the one attached) into blocks/autonav/templates

It is a bit of a mess as the code lines don't wrap above, but I attach the complete breadcrumbs.php file I use also. (saved as breadcrumbs.txt)
jizzle replied on at Permalink Reply
jizzle
You don't need to create a block... well, maybe not... just use CSS to create a breadcrumb. Use Concrete5's autonav for the breadcrumb, and style the UL and LI stylesheet appropriately. You can find tutorials on this... just search google.
Slacker replied on at Permalink Reply
Slacker
do you have a sample of a working CSS breadcrumb? I would love to see it
plschneide replied on at Permalink Reply
plschneide
I am hoping someone can help some more with the CSS. I've been searching all over and while I have found code that will display and flatten out the UL list, it does NOT flatten out when a child UL list is displayed.

In my site I have several pages "nested" inside folders to group them together and then the "sub" pages are listed on a 2nd line in the bread crumb.

Right now the CSS I am using is (below) like I said this works great for the first tier but not subsequent ones - ideas????



#page .bread ul li {
display:inline;
list-style:none;
font-size: 12px;
margin-bottom: 10px;
padding-top: 2px;
padding-bottom: 10;
padding-left: 10px;
margin-top: 10px;
padding-right:10px; /*width of img + a bit*/
}

#page .bread li:after {
margin-left: 3px;
margin-right: 3px;
content: "/";
}

#page .bread li:last-child:after {
content: "";
padding-right:none;
}
enque replied on at Permalink Reply
enque
Thanks for this.
The bread crumb is showing up well.
The problem I am having is for pages with show next link checked.

eg. Ministries is the parent but when clicked goes directly to the first child page.

Using breadcrumbs it goes to the parent page and not the first child like it should.
I'd be grateful for any suggestions or extension to this snippet.
ob7dev replied on at Permalink Reply
ob7dev
Version 5.7 (display breadcrumb in code:)
$autonav = BlockType::getByHandle('autonav');
$autonav->controller->orderBy = 'display_asc';
$autonav->controller->displayPages = 'all';
$autonav->controller->displaySubPages = 'relevant_breadcrumb';
$autonav->controller->displaySubPageLevels = 'all';
$autonav->render('templates/breadcrumb');