4

In Drupal 8, if I create a node, add a menu link, save it, then go back and edit the menu link title in the node, the breadcrumb title does not update until I clear the cache.

How can this be set so it reflects this change without requiring flushing the cache?

4
  • You can invalidate the specific cache when the title changes.
    – Eyal
    Dec 10, 2016 at 12:26
  • To find which cache tag to invalidate I suggest looking at the cachetags db table and finding the one that sounds most appropriate for breadcrumbs. It could also be the cachetag for the block.
    – Eyal
    Dec 10, 2016 at 12:27
  • 1
    I've checked this, there are no tags. See my answer.
    – 4uk4
    Dec 10, 2016 at 13:40
  • @Eyal: You don't need to invalidate yourself. If you have set the correct cache tag in the breadcrumb, it will get invalidated automatically when the entity is saved.
    – 4uk4
    Dec 10, 2016 at 14:10

2 Answers 2

15

Breadcrumbs for nodes have no cache tags. The reason is that the node entity has no specific breadcrumb builder. The generic path based breadcrumb builder gets the title from the title resolver and is not aware where the title is from and what tag it should set.

You can add tags for nodes in hook_system_breadcrumb alter():

function mymodule_system_breadcrumb_alter(\Drupal\Core\Breadcrumb\Breadcrumb &$breadcrumb, \Drupal\Core\Routing\RouteMatchInterface $route_match, array $context) {
  if ($route_match && $node = $route_match->getParameter('node')) {
    $breadcrumb->addCacheableDependency($node);
  }
}

To test this you need to have extended the breadcrumb behavior to display the title. In my test site this was the Bootstrap theme. Another option would be the module Current Page Crumb.

7
  • Now that you mention it, I did install Menu Breadcrumb, it suited me better than CPC.
    – Kevin
    Dec 11, 2016 at 0:21
  • In this case you need $breadcrumb->addCacheableDependency($entity) for MenuLinkContent where this module gets the title from. I would open an issue.
    – 4uk4
    Dec 11, 2016 at 8:34
  • Wow, that did it. Nice. Whats the best documentation on how caching/cache tags/cache context works with real world examples?
    – Kevin
    Dec 12, 2016 at 14:45
  • dear @4k4, it would be great to use that code segment in Menu Breadcrumb but we still have no way of converting all menu link plugin IDs in the active trail into entities. Please see Menu Breadcrumb issue 2831084 for a counter-example. The general difficulty is phrased here. If we address that issue, this module will finally be ready for a full release.
    – rphair
    Dec 12, 2016 at 16:06
  • @rphair, don't think that all menu links have entities. I've posted what I found in the issue: drupal.org/node/2827653#comment-11822434
    – 4uk4
    Dec 12, 2016 at 17:38
4

I am very sorry that I don't (yet) have the "50 reputation" to comment on 4x4's excellent answer above, but we have a pathogical case already identified in that same issue queue...


It would be great to pop that code segment into Menu Breadcrumb but we still have no way of converting all menu link plugin IDs in the active trail into entities. Please see Menu Breadcrumb issue 2831084 for a counter-example. The general difficulty is phrased here and if we find an answer there would finally be a release candidate for Menu Breadcrumb.

For relevance to the original posting, once that happens (i.e. we know how to produce a cacheable entity for all items on the menu active trail) I will come back & modify this answer if it's not already posted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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