Skip to content

Commit

Permalink
logic to handle breadcrumbs when deleting a node
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-gao committed Jan 9, 2025
1 parent 4c893ce commit baf7e2c
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion modules/tide_site/tide_site.module
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function tide_site_entity_presave(EntityInterface $entity) {
}
}
if ($entity instanceof NodeInterface) {
if (!$entity->field_breadcrumb_parent->isEmpty()) {
if ($entity->hasField('field_breadcrumb_parent') && !$entity->field_breadcrumb_parent->isEmpty()) {
$bm = new BreadcrumbCacheDependencyManager();
$bm->addDependency($entity->id(), $entity->field_breadcrumb_parent->target_id);
$affected_nodes = $bm->getAllAffectedNodes($entity->id());
Expand All @@ -140,6 +140,35 @@ function tide_site_entity_presave(EntityInterface $entity) {
Cache::invalidateTags($tags);
}
}
if ($entity->hasField('field_breadcrumb_parent') && $entity->field_breadcrumb_parent->isEmpty()) {
try {
$entity->set('field_breadcrumb_parent', $entity->field_node_primary_site->entity->field_site_homepage->entity->id());
$bm = new BreadcrumbCacheDependencyManager();
$bm->addDependency($entity->id(), $entity->field_node_primary_site->entity->field_site_homepage->entity->id());
$affected_nodes = $bm->getAllAffectedNodes($entity->id());
$tags = $bm->NodeIdsToNodeCacheTags($affected_nodes);
if (!empty($tags)) {
Cache::invalidateTags($tags);
}
}catch (\Exception $e) {
\Drupal::messenger()->addMessage($e->getMessage());
}
}
}
}

/**
* Implements hook_ENTITY_TYPE_delete().
*/
function tide_site_node_delete(NodeInterface $node) {
if ($node->hasField('field_breadcrumb_parent')) {
$bm = new BreadcrumbCacheDependencyManager();
$affected_nodes = $bm->getAllAffectedNodes($node->id());
$tags = $bm->NodeIdsToNodeCacheTags($affected_nodes);
if (!empty($tags)) {
$bm->removeNode($node->id());
Cache::invalidateTags($tags);
}
}
}

Expand Down

0 comments on commit baf7e2c

Please sign in to comment.