From 3e957bf21eb2bcfb773fc3437fa8c0b1f7cba93d Mon Sep 17 00:00:00 2001 From: Josh Crawford Date: Sun, 20 Jun 2021 13:15:23 +1000 Subject: [PATCH] Fix when turning off node propagation, duplicate nodes would occur --- src/services/Navs.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/services/Navs.php b/src/services/Navs.php index 90450d2..ad3bf89 100644 --- a/src/services/Navs.php +++ b/src/services/Navs.php @@ -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) @@ -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 {