I have content types Parent and Child. Parent is a referenced entity in Child. My Parent template lists all children with reference to this parent.
After updating the parents referenced in a Child node, the Parent template does not show the change, unless I manually clear all caches. Disabling render cache globally solves the problem. It seems that Parent cache does not invalidate on updating the Child node. Is this behavior by design? How can I fix it?
Drupal ver. 10.1.0
I use the preprocess_node hook to pass children to the parent template:
function mytheme_preprocess_node(&$variables)
{
// Check if the current node is of type 'parent'
if ($variables['node']->getType() == 'parent') {
// Load all 'children' nodes referencing the current 'parent' node
$query = \Drupal::entityQuery('node')
->accessCheck(FALSE)
->condition('type', 'child')
->condition('field_child_parent', $variables['node']->id());
$children_ids = $query->execute();
// Load the 'children' nodes
$children = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($children_ids);
// Pass the 'children' nodes to the template.
$variables['children'] = $children;
}
}
Following comment by @4uk4 I added:
$variables['#cache']['tags'][] = 'node_list';
This is working, but is it the best solution in terms of performance?