Saturday 12 March 2011

Drupal breadcrumb custom settings

Some time ago, working on Dev-factory project project, I had a problem to display customized breadcrumb in a way that user can navigate back to parent page while reading child page of specific menu.
Please have a look at this Child Page, as you can see breadcrumb just below the main navigation displays option to navigate back to parent Menu items, and this is only on pages where menus have children.
Took I vile to get decision and here is how I solve it;
1. We need to set active navigation to web sites Main menu - Primary links at our case (Remember Drupal`s default menu is Navigation). In order to make it properly Let's set up custom module so Drupal knows when and what to do. (Here is link about what files you need for Drupal module - I will no go deeper, only showing the modules code).

/**
* Implementation of hook_init().
*/
function breadcrumb_init() {
// Set active menu to Primary, when not on admin pages.
if (arg(0) != 'admin' && arg(1) != 'add') {
menu_set_active_menu_name('primary-links');
}
}

So by using hook_init I am telling that if not admin page or not node adding page Drupal need to switch to primary-links.
Next step theming - here is a function you can use in your template.php file naming it - yourthemename_breadcrumb ;

function energetic_breadcrumb($breadcrumb) {
$breadcrumb = menu_get_active_breadcrumb();
if (!empty($breadcrumb)) {
$title = drupal_get_title();
unset($breadcrumb[0]);
if (arg(0) == 'admin' || arg(0) == 'user') {
return '';
}
if (arg(0) == 'node' && arg(1) == 'add') {
return '';
}
if (arg(0) == 'search') {
return;
}
else {
return '<h4 class="breadcrumb">' . implode(' > ', $breadcrumb) . ' > ' . $title . '</h4>';
}
}
}

The result you can see on Page I mentioned above.
Took me couple of hours at first time to get it ready, but looks good and problem solved.

No comments:

Post a Comment