Skip to main content

Drupal 8: Missing breadcrumbs in active trail

Submitted by callum on 06 Feb 2019
work drupal

Problem:

On a Drupal 8 build, breadcrumb trails were not always showing the full active path and at some point in the active menu trail, the trail stopped. Some nodes worked while others did not.

 

Partial Solution:

My initial suspicion fell on my themename.theme hook_preprocess_breadcrumb( &$variables ), to add the current page title as the last element in the breadcrumb array:

function themename_preprocess_breadcrumb(&$variables) { 
  if (($node = \Drupal::routeMatch()->getParameter('node')) && $variables['breadcrumb']) { 
    // Adding the title of the page in the breadcrumb 
    $variables['breadcrumb'][] = array( 'text' => $node->getTitle() );
    $variables['#cache']['contexts'][] = 'url';
  }
}

I only needed this for the node content, so this suited my purposes.

 

$variables['#cache']['contexts'][] = 'url';

prevents the breadcrumbs caching the title on every page.

 

When I removed hook_preprocess_breadcrumb( &$variables ), the title vanished from the breadcrumb listing, but it did not fix the problem of the missing trail.

 

Drupal 8 builds breadcrumbs trails based on the url path. My default url alias pattern uses the node title [/admin/config/search/path/patterns]:

[node:menu-link:parents:join-path]/[node:title]

The nodes that have both node title and the menu link title the same, worked as expected. The nodes that had different node/menu item titles did not work and the trail stopped at this point.

To fix, I updated my path alias pattern to use the menu link title as follows:

[node:menu-link:parents:join-path]/[node:menu-link:title]

Then bulk rebuild all the affected url aliases [/admin/config/search/path/update_bulk]. Then cleared the cache and the breadcrumbs now work as expected.

If a node has a custom URL, instead of the generated one, we are back to the same problem. So it seems the URL path is quite important for the breadcrumb trail, instead of the menu trail path.