1

I have a site where the breadcrumb is in different locations depending on what node type or page is being viewed.

In some places, it is interspersed with node content, so the only solution was for my colleague to inject it into the page as a variable, like so (from .theme):

$block = Block::load('breadcrumbs');
$variables['content']['breadcrumbs'] = \Drupal::entityTypeManager()->getViewBuilder('block')->view($block);

I've noticed that when I deploy code, the breadcrumbs disappear. I then have to clear the cache, clear Varnish, and clear the cache again and finally hard refresh in my browser to see them again.

Is this related to how breadcrumbs are being injected into the page and if so, is there a better way to do it?

  • What hook is that code in inside your .theme file? Also in D8 you can assign a block to more than one region... could you just put the crumbs block in both places it's required and then just hide one with css? – Felix Eve Feb 1 '18 at 0:11
  • Depending on the page they can show up within node content area, not just a region, like in between fields. This is in hook preprocess node – Kevin Feb 1 '18 at 0:12
2

In a node use the breadcrumb builder directly:

\Drupal::service('breadcrumb')->build(\Drupal::routeMatch())->toRenderable();

The node gets cached already and the cache metadata of the breadcrumbs bubbles up to the node.

In a page it's preferable to place the block in a region (can be a region of its own) and then place the variable of the region in the page template.

If you want to use code to place the block, make sure you use the block for the correct theme (you have to query the block table with the property theme for this). Now it seems like you use the breadcrumb block placed first on your site, not necessarily the one for your theme.

  • This helped with that particular node type, but I see it on others - updated the question with our breadcrumb build() method. – Kevin Feb 1 '18 at 14:55
  • Don't think this is connected, you don't even need the cache data you provide now, if you embed the breadcrumbs in the node. – 4k4 Feb 1 '18 at 16:35
  • This one is actually served from the core block. I'll make a new question. – Kevin Feb 1 '18 at 16:39

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.