Score:0

Parent render cache not clearing on child node update

ni flag
rhy

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?

id flag
What is the Drupal version? Assuming 9 or 10, this is because there is missing cache metadata. Are there any custom modules? What template hooks are implemented in your custom theme?
4uk4 avatar
cn flag
See https://drupal.stackexchange.com/questions/246514/get-referenced-node-fields-in-twig-where-reference-is-on-remote-node-only
id flag
You should reference the specific nodes like `node:42` for the best performance.
4uk4 avatar
cn flag
`node:42` is not enough because the reference to a parent node can be added without modifying the parent. You need the list tag, but now you can use a content type specific list tag which improves cache performance: `node_list:content_type_child`
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.