1

I run in a strange issue with breadcrumb modifying. In my template, I created a hook to show the current page in the breadcrumbs (for testing):

function business_preprocess_breadcrumb(&$variables){

if(($node = \Drupal::routeMatch()->getParameter('node')) && $variables['breadcrumb']){
    $variables['breadcrumb'][] = array(
                    'text' => $node->getTitle(),
                    'url' => $node->URL()
                   );
     }

}

The code above works after clearing the cache and loading the first node page. However, as soon as I load any other node page, it's always showing the title of the first loaded node (like it is cached). Even when I echo the Node id, I always see the same ID from the first node. How can I fix this? I even tried to create a custom module which overrides the breadcumb generation - same result here.

Drupal 8.1.1.

2

Change the cache context from 'url.path' to 'url'

function xxx_preprocess_breadcrumb(&$variables) {
    ...
    $variables['breadcrumb'] = array(
      array(
        'text' => 'some text',
        'url' => "/someurl"
      )
    );       
    ...
    $variables['#cache']['contexts'][] = 'url';         
}

I ran into the same problem with Drupal 8.2.4

1
  • This actually is a comment to an already existing answer. Would be nice to add that information to your answer.
    – leymannx
    Dec 9 '18 at 18:23
1

In Drupal 8.1.1 the cache context for breadcrumbs was changed from url.path to url.path.parent to reduce the cache data for the breadcrumbs block.

Issue: url.path cache context for breadcrumbs is unnecessarily granular

After the change in core the title of the node gets cached on the first request and then all subsequent requests with the same path parent, in this case /node/, will get the same result from cache.

You can put the old context back in breadcrumbs in this hook:

function hook_system_breadcrumb_alter(\Drupal\Core\Breadcrumb\Breadcrumb &$breadcrumb, \Drupal\Core\Routing\RouteMatchInterface $route_match, array $context) {
  $breadcrumb->addCacheContexts(['url.path']);
}

As alternative you can set the context in a render array in preprocess:

'#cache' => [
    'contexts' => [
      'url.path',
    ],
4
  • Hi, thanks for the hint. Unfortunately, it doesn't make a difference (it also seems like the system_breadcrumb_alter is not called (at least a basic "echo" in it does not show up). I should also note that I had the same behaviour in Drupal 8.1.0
    – TSGames
    May 25 '16 at 19:54
  • If the alter hook doesn't work you can locate the cache context in preprocess and see what's in there and change it so that it is matching your dynamic data. That is not specific to breadcrumbs.
    – 4k4
    May 25 '16 at 20:32
  • How can I access the Breadcrumb object in the preprocess function? The $variables['breadcrumb'] is just an array.
    – TSGames
    May 26 '16 at 8:33
  • In preprocess you have a render array, see the edited answer.
    – 4k4
    May 26 '16 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.