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 5f7dc65
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
6 changes: 4 additions & 2 deletions modules/tide_site/tide_site.install
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

use Drupal\Component\Utility\NestedArray;
use Drupal\tide_site\TideSiteFields;

/**
* Implements hook_install().
Expand Down Expand Up @@ -98,6 +99,7 @@ function tide_site_update_10003() {
$system_feature_flags->set('linkset_endpoint', TRUE)
->save();
}

/**
* Add breadcrumb fields to all node bundles.
*/
Expand All @@ -106,9 +108,9 @@ function tide_site_update_10004() {
$fields_helper = \Drupal::service('tide_site.fields');
$node_bundles = Drupal::entityTypeManager()->getStorage('node_type')->loadMultiple();
foreach ($node_bundles as $bundle_id => $entity) {
$fields_helper->provisionField(\Drupal\tide_site\TideSiteFields::FIELD_BREADCRUMB_NAME,
$fields_helper->provisionField(TideSiteFields::FIELD_BREADCRUMB_NAME,
'node', $bundle_id);
$fields_helper->provisionField(\Drupal\tide_site\TideSiteFields::FIELDD_BREADCRUMB_PARENT,
$fields_helper->provisionField(TideSiteFields::FIELDD_BREADCRUMB_PARENT,
'node', $bundle_id);
}
}
51 changes: 43 additions & 8 deletions modules/tide_site/tide_site.module
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
* Tide Site module functionality.
*/

use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\tide_site\Plugin\Field\BreadcrumbField;
use Drupal\tide_site\BreadcrumbCacheDependencyManager;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Link;
Expand All @@ -26,11 +25,12 @@ use Drupal\path_alias\Entity\PathAlias;
use Drupal\path_alias\PathAliasInterface;
use Drupal\search_api\IndexInterface;
use Drupal\taxonomy\Entity\Term;
use Drupal\tide_site\BreadcrumbCacheDependencyManager;
use Drupal\tide_site\Plugin\Field\BreadcrumbField;
use Drupal\tide_site\TideSiteFields;
use Drupal\tide_site\TideSiteMenuAutocreate;
use Drupal\tide_site\TideSitePathAliasListBuilder;
use Drupal\views\ViewExecutable;
use Drupal\Core\Cache\Cache;

/**
* Implements hook_entity_bundle_create().
Expand All @@ -51,7 +51,12 @@ function tide_site_entity_bundle_create($entity_type_id, $bundle) {

// Map of which fields should be created for which entity types.
$map = [
'node' => [$fields_helper::FIELD_SITE, $fields_helper::FIELD_PRIMARY_SITE, $fields_helper::FIELD_BREADCRUMB_NAME, $fields_helper::FIELDD_BREADCRUMB_PARENT],
'node' => [
$fields_helper::FIELD_SITE,
$fields_helper::FIELD_PRIMARY_SITE,
$fields_helper::FIELD_BREADCRUMB_NAME,
$fields_helper::FIELDD_BREADCRUMB_PARENT,
],
'media' => [$fields_helper::FIELD_SITE],
];

Expand Down Expand Up @@ -131,7 +136,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 +145,36 @@ 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 5f7dc65

Please sign in to comment.