Better breadcrumbs for your Drupal 7 site

Often times when creating custom modules for Drupal, we get confronted with the problem of setting friendly but useful breadcrumbs. The contribution a clearly set breadcrumb can make on a page can never be understated, it guides the user on where they’re coming from and currently are.
This post assumes you already know how to write a module for drupal and you’re pretty familiar with the drupal way of coding.

Today i’m going to show drupal developers how to use internal and contributed breadcrumbs API to create modules that use clearly defined breadcrumb rules. This will ultimately help webmasters and site creators when they use your modules, besides making Drupal super cool to use.

First off, download the awesome Custom Breadcrumbs module that comes bundled with the API we’re going to use.

Create a custom module.

The following module is a super simple module to illustrate the concept, lets call it newcrumbs. Here’s our .info file:

name = newcrumbs
description =Custom module that uses a breadcrumb API
core =6.x
package=Other

Now that we are done declaring our module, let’s dive into code.

Next we call our module straight from the pages generated by our module. Ideally, these pages we would create using hook_menu() (maybe by other modules) so let’s go ahead and define that.

<?php
/**
  * Implementation of hook_menu()
  */
  
function newcrumbs_menu(){
 $items = array();
 $items['tealist']= array (
    'title'= t('A listing of different kinds of tea'),
    'page callback'= 'displaytea_list',
    'access arguments'= array('administer content'),
    'type'= MENU_CALLBACK,
 );
  
 return $items;
}

Now for the callback that will be called on our newly created page.

<?php
function displaytea_list(){
 //let's define a breadcrumb for the page here
 $breadcrumb = array();
 drupal_alter('breadcrumb', $breadcrumb,'newcrumbs_call');
 //just before setting it, we invoke the module.
}

Now back into our admin section, go to “admin/build/custom_breadcrumbs/module/add” and down the list, you should see our newly created module. Go ahead and define the rules to use and VOILA! The module now uses well defined, easy to read breadcrumbs!

Tags