Custom breadcrumbs by taxonomy term using visibility PHP

When setting up a custom breadcrumb trail using the Custom Breadcrumb module you can determine whether the breadcrumb should be displayed by using a snippet of PHP to return TRUE or FALSE. This code has access to the $node variable, and can be used to check things like it's type, or in this instance what taxonomy term is associated with a node.

For this example we have a content type of Case Studies and when creating a node of this type there is an option to select what Category it belongs to from a list of taxonomy terms

The Case Studies are displayed on different pages under their particular Category. Because of this we want to have custom breadcrumb trails leading back to the Category page a particular Case Study belongs to.

Creating the breadcrumb visibility rule

You need to check what Category value is associated with the Case Study you are viewing. Easiest way to find the correct syntax is by using the devel module - see this post about finding field attributes for more info.

Once you have found out how to reference and check the Category field value you will need to find out what the value is. In this example I'm using the TID of the taxonomy terms, and you can find this by going to the taxonomy list and hovering over the edit button - you will see something like this .../taxonomy/term/12/edit     (tid=12 in this example).

You can then enter code similar to this in the breadcrumb visibility area to get it appearing for Case Studies tagged with that Category tid.
IMPORTANT - do not include the <?php ?> tags

if ($node->field_section['und'][0]['taxonomy_term']->tid == 12) return TRUE;
Drupal version: 
Justin Chevallier

Justin Chevallier

Avid Drupal site builder & user for +10 years.

Comments

Submitted by :) on

Thanks ! It's difficult to find snippet example. How to create a rule multifilter ? I need to display the breadcrumb if node has field taxo-reference-tid & taxo2-tid.

Thank you

Submitted by Justin on

If I understand you correctly you should be able to do something like the below to show the breadcrumb with two things that need to return true:

if ( ($node->field_taxo_reference_tid['und'][0]['taxonomy_term']->tid == 12) && (
$node->field_taxo2_tid['und'][0]['taxonomy_term']->tid == 5
) ) return TRUE;

Submitted by Eric on

I am getting a 'Notice: Trying to get property of non-object in eval()' notice on pages using this...

Submitted by Eric Aitala on

This is fixed by doing >>

if (($node->field_archival) && $node->field_archival['und'][0]['tid'] == 32)) return TRUE;

Because if there are node with no taxonomy terms applied, it tosses the notice/warning.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.