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.

Questions & Answers

What is the best web development framework?
Many people commonly ask “what is a framework in web development?” Web development frameworks can easily be confused with web development tools, languages, or parts of the web development stack (like .NET, PHP, JavaScript, or Ruby).
Learn More about What is the best web development framework?
What is the best programming language for web development?
If there was one “best” programming language, then everything else would be obsolete. The reality is that there are so many different programming languages because there is no “best” language for any situation.
Learn More about What is the best programming language for web development?
How much does web development cost?
Web development can vary from a few hundred to millions of dollars depending on what is needed. You may simply need some changes to something that already exists, or you'd like to build a large or complex application.
Learn More about How much does web development cost?
What is web design and development?
People often lump web design and development together, so what's the difference? As the Internet has evolved, the skills required to produce a high quality website or web application have changed.
Learn More about What is web design and development?
What is JavaScript used for in web development?
Historically speaking, JavaScript was only commonly but sparingly used in web development. The multiple browsers in use at the time each supported different versions of JavaScript and were slow to render more complex Javascript.
Learn More about What is JavaScript used for in web development?
What is React web development?
React is a popular JavaScript library. It is primarily used for building interactive user interfaces (UI).
Learn More about What is React web development?
What is PHP web development?
PHP is a back end language primarily used for custom applications, content management systems (such as Wordpress), eCommerce engines (such as Magento), or even massive sites like Facebook.
Learn More about What is PHP web development?