Skip to content

Commit

Permalink
Fix when turning off node propagation, duplicate nodes would occur
Browse files Browse the repository at this point in the history
  • Loading branch information
engram-design committed Jun 20, 2021
1 parent 868342e commit 3e957bf
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/services/Navs.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ public function handleChangedNav(ConfigEvent $event)
if (!$navRecord->propagateNodes && $oldRecord->propagateNodes) {
$primarySiteId = Craft::$app->getSites()->getPrimarySite()->id;
$nav = $this->getNavById($navRecord->id);
$elementsService = Craft::$app->getElements();

$nodes = Node::find()
->navId($navRecord->id)
Expand All @@ -283,11 +284,21 @@ public function handleChangedNav(ConfigEvent $event)
->all();

foreach ($nav->getEditableSites() as $site) {
// No need to re-save the primary site
// No need to re-save the primary site, it's all good as-is
if ($site->id == $primarySiteId) {
continue;
}

// If we try and propagate nodes to another site's nav, which already
// has nodes, we'll get duplicates. As there's no real way to compare
// propagated and non-propagated nodes (effectively), we need to wipe all
// other enabled nav nodes first, before duplicating.
$existingNodes = Node::find()->siteId($site->id)->all();

foreach ($existingNodes as $existingNode) {
$elementsService->deleteElement($existingNode);
}

$this->_duplicateElements($nodes, ['siteId' => $site->id]);
}
} else {
Expand Down

0 comments on commit 3e957bf

Please sign in to comment.