diff --git a/modules/tide_publication/src/Navigation/Children.php b/modules/tide_publication/src/Navigation/Children.php index e90fa9ac2..705ef5330 100644 --- a/modules/tide_publication/src/Navigation/Children.php +++ b/modules/tide_publication/src/Navigation/Children.php @@ -5,7 +5,7 @@ use Drupal\Core\Cache\CacheableMetadata; /** - * Class Root. + * Class Children for navigation. */ class Children extends Base { @@ -44,6 +44,10 @@ protected function computeValue() { $this->list[] = $this->createItem($weight, ['target_id' => $child_id]); } + + // Add the collected cacheable metadata to the parent entity. + // If any child changes, the cache for this field is invalidated. + $entity->addCacheableDependency($cache); } } diff --git a/modules/tide_publication/src/Navigation/Next.php b/modules/tide_publication/src/Navigation/Next.php index 75ff62b9d..e2d126222 100644 --- a/modules/tide_publication/src/Navigation/Next.php +++ b/modules/tide_publication/src/Navigation/Next.php @@ -31,11 +31,16 @@ protected function computeValue() { $next = $delta + 1; if (isset($hierarchy[$next])) { $this->list[] = $this->createItem(0, ['target_id' => $hierarchy[$next]['entity_id']]); - return; + // Break the loop and proceed to add cache metadata. + break; } } } } + + // Add the collected cacheable metadata to the parent entity. + // Make sure field invalidates if the root or any hierarchy node changes. + $entity->addCacheableDependency($cache); } } diff --git a/modules/tide_publication/src/Navigation/Previous.php b/modules/tide_publication/src/Navigation/Previous.php index 59fb55c1f..2d4b677a9 100644 --- a/modules/tide_publication/src/Navigation/Previous.php +++ b/modules/tide_publication/src/Navigation/Previous.php @@ -5,7 +5,7 @@ use Drupal\Core\Cache\CacheableMetadata; /** - * Class Next. + * Class Previous for navigation. */ class Previous extends Base { @@ -31,11 +31,16 @@ protected function computeValue() { $prev = $delta - 1; if (isset($hierarchy[$prev])) { $this->list[] = $this->createItem(0, ['target_id' => $hierarchy[$prev]['entity_id']]); - return; + // Use break instead of return so we reach the cache dependency call below. + break; } } } } + + // Attach the collected cache metadata to the entity. + // Tells Drupal field depends on all nodes in the hierarchy. + $entity->addCacheableDependency($cache); } }