I have for example Node A with alias node-a and Node B with alias node-a/node-b so on my Node B i have this breadcrumb Home > Node A > Node B
GraphQl result:
"breadcrumb": [
{
"text": "Home",
"url": {
"path": "/",
"routed": true
}
},
{
"text": "Node A",
"url": {
"path": "http://example.com/node-a",
"routed": true
}
},
{
"text": "Node B",
"url": {
"path": "",
"routed": true
}
}
]
But when i change Node A title i get always the same result (breadcrumbs not changed), but when i re-save the node A or clear cache the graphQl result change.
Here is what i have tried:
- Clear graphQl cache (results and definitions)on node update:
/**
* Implements hook_ENTITY_TYPE_update().
*/
function MYMODULE_node_update(EntityInterface $entity) {
// Clear graphql cache.
/** @var \Drupal\Core\Cache\CacheBackendInterface $graphql_results_cache */
$graphql_results_cache = \Drupal::service('cache.graphql.results');
$graphql_results_cache->invalidateAll();
/** @var \Drupal\Core\Cache\CacheBackendInterface $graphql_definitions_cache */
$graphql_definitions_cache = \Drupal::service('cache.graphql.definitions');
$graphql_definitions_cache->invalidateAll();
}
it doesn't work !
- Clear cache of all child node on node update like this:
/**
* Implements hook_ENTITY_TYPE_update().
*/
function MYMODULE_node_update(EntityInterface $entity) {
$database = \Drupal::database();
// Get the current node path alias.
$alias = \Drupal::service('path_alias.manager')
->getAliasByPath('/node/' . $entity->id());
// Get all node child of the current node.
$child_nodes_alias = $database->select('path_alias', 'pa')->fields('pa', [
'path',
'alias',
])->condition('path', '/node/%', 'LIKE')
->condition('alias', $alias . '%', 'LIKE')
->execute()
->fetchAll();
// Invalidate cache for each node child of current node.
foreach ($child_nodes_alias as $alias_data) {
$nid = explode('/', $alias_data->path)[2] ?? NULL;
if ($nid && $nid != $entity->id()) {
$tags = ['node:' . $nid];
Cache::invalidateTags($tags);
\Drupal::entityTypeManager()->getStorage('node')->resetCache([$nid]);
}
}
}
it doesn’t work also
- I combined the two previous ways it doesn't work too.
When i clear all caches on node update it works, but it's not the best way it impact site cache.
/**
* Implements hook_ENTITY_TYPE_update().
*/
function MYMODULE_node_update(EntityInterface $entity) {
drupal_flush_all_caches();
}
What is the best way to clear breadcrumbs cache, or child nodes without clear all site cache ?
Edit:
Thank you @4K4 for your comment, i did tried to add CacheTags too breadcrumb but i didn’t work either !
/**
* Implements hook_system_breadcrumb_alter().
*/
function MYMODULE_system_breadcrumb_alter(Breadcrumb &$breadcrumb, RouteMatchInterface $route_match, array $context) {
// Append the current page title to the breadcrumb for non-admin routes.
if ($breadcrumb && !\Drupal::service('router.admin_context')->isAdminRoute()) {
$links = $breadcrumb->getLinks();
foreach ($links as $link) {
$parameters = $link->getUrl()->getRouteParameters();
if ($parameters) {
// Make sure the breadcrumb is updated when node title changes.
$breadcrumb->addCacheTags(['node:' . $parameters['node']]);
$breadcrumb->addCacheContexts(['url']);
}
}
}
}
Note: Easy Breadcrumb module installed.