Skip to content
Article

How to add custom fields to Drupal 7 breadcrumb

Breadcrumbs in Drupal 7 are normally based on the menu title.  However, what if you wanted different breadcrumb text than what appears in the menu?  For example, you have a small space and long titles, and would rather use abbreviations in your breadcrumb to save space (usability concerns aside).
I’m aware of modules like Custom Breadcrumb, but I wasn’t able to find anything that allowed me to:

  1. Create a custom field on a node
  2. Optionally use that field (if populated) instead of the menu title in the breadcrumb list

So, add the following to your template.php file after creating a node field field_breadcrumb. If you have a better idea, let me know in the comments!

Based on digging through hook_menu_breadcrumb_alter and menu_get_active_breadcrumb.
[php]

/**
* Reconfigure the breadcrumb trail to use each parent’s custom field
* @see hook_menu_breadcrumb_alter
* @see menu_get_active_breadcrumb
*
* @param array $active_trail the current page active trail
* @param whatever $item the current trail item
*/
function YOURTHEME_menu_breadcrumb_alter(&$active_trail, $item) {

// must step through each menu item, check to see if it has the ‘custom breadcrumb’ field
foreach( $active_trail as &$crumb ) :
$node_id = str_replace(‘node/’, ”, $crumb[‘link_path’]);
if( $node_id ) :
// get the node to find the field
$node = node_load($node_id);
// best…function…ever
$crumb_text = field_get_items(‘node’, $node, ‘field_breadcrumb’);

// replace existing text if we should
if( $crumb_text ) :
$crumb[‘link_title’] = $crumb_text[0][‘safe_value’];
$crumb[‘title’] = $crumb_text[0][‘safe_value’];
endif;

endif;
endforeach;

// add a sacrificial final item, since that gets removed later on
$active_trail []= $crumb;

}//– fn YOURTHEME_menu_breadcrumb_alter

/**
* Return a themed breadcrumb trail.
*
* Implements Zen theme `zen_breadcrumb`. Paired with XYZ_menu_breadcrumb_alter, which appended a sacrificial duplicate
*
*
* @param $variables
* – title: An optional string to be used as a navigational heading to give
* context for breadcrumb links to screen-reader users.
* – title_attributes_array: Array of HTML attributes for the title. It is
* flattened into a string within the theme function.
* – breadcrumb: An array containing the breadcrumb links.
* @return
* A string containing the breadcrumb output.
*/
function YOURTHEME_breadcrumb($variables) {
if( isset( $variables[‘breadcrumb’]) && !empty( $variables[‘breadcrumb’] )) :
// Don’t show a link to the current page in the breadcrumb trail.
// this should automatically take into account theme settings…
$last_key = end( array_keys( $variables[‘breadcrumb’] ) );
$end = end( $variables[‘breadcrumb’] );
$current_path = url(request_path(), array(‘absolute’ => false));
if ( false !== strpos($end, $current_path) ) {
$variables[‘breadcrumb’][$last_key] = strip_tags( $end );
}
endif;
// business as usual
return zen_breadcrumb($variables);
}//– fn YOURTHEME_breadcrumb
[/php]
Todo: make this a module…

The Atlantic BT Manifesto

The Ultimate Guide To Planning A Complex Web Project

Insights

Atlantic BT's Insights

We’re sharing the latest concepts in tech, design, and software development. Learn more about our findings.