From 3825db715fdd122fdbca6b0add1f1bc78d1b92a9 Mon Sep 17 00:00:00 2001 From: Prokyonn Date: Tue, 6 Sep 2022 14:52:06 +0200 Subject: [PATCH 1/2] Add support for unlocalized navigation contexts --- EventSubscriber/NavigationInvalidationSubscriber.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/EventSubscriber/NavigationInvalidationSubscriber.php b/EventSubscriber/NavigationInvalidationSubscriber.php index 5d60fec..5135f3c 100644 --- a/EventSubscriber/NavigationInvalidationSubscriber.php +++ b/EventSubscriber/NavigationInvalidationSubscriber.php @@ -116,12 +116,14 @@ public function collectNavigationContextBeforeRemovingLocale(RemoveLocaleEvent $ $this->collectNavigationContexts($path, $event->getLocale()); } - public function collectNavigationContexts(string $path, string $locale): void + public function collectNavigationContexts(string $path, ?string $locale): void { $defaultNode = $this->defaultSession->getNode($path); $liveNode = $this->liveSession->getNode($path); - $propertyName = $this->propertyEncoder->localizedContentName('navContexts', $locale); + $propertyName = $locale ? + $this->propertyEncoder->localizedContentName('navContexts', $locale) : + $this->propertyEncoder->contentName('navContexts'); $liveNavigationContexts = []; $defaultNavigationContexts = []; if ($liveNode->hasProperty($propertyName)) { From 7dbc0d1333f16519a2e9de7f9ce28b2888faba5f Mon Sep 17 00:00:00 2001 From: Luca Rath-Heel Date: Mon, 19 Sep 2022 12:36:18 +0200 Subject: [PATCH 2/2] Implement suggestions from code review --- .../NavigationInvalidationSubscriber.php | 56 +++++++++---------- UPGRADE.md | 4 ++ 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/EventSubscriber/NavigationInvalidationSubscriber.php b/EventSubscriber/NavigationInvalidationSubscriber.php index 5135f3c..135bdb3 100644 --- a/EventSubscriber/NavigationInvalidationSubscriber.php +++ b/EventSubscriber/NavigationInvalidationSubscriber.php @@ -91,53 +91,53 @@ public static function getSubscribedEvents() public function collectNavigationContextBeforePublishing(PublishEvent $event): void { - $path = $this->documentInspector->getPath($event->getDocument()); - $this->collectNavigationContexts($path, $event->getLocale()); + $this->collectNavigationContexts($event->getDocument(), $event->getLocale()); } public function collectNavigationContextBeforeUnpublishing(UnpublishEvent $event): void { - $path = $this->documentInspector->getPath($event->getDocument()); - $this->collectNavigationContexts($path, $event->getLocale()); + $this->collectNavigationContexts($event->getDocument(), $event->getLocale()); } public function collectNavigationContextBeforeRemoving(RemoveEvent $event): void { - $document = $event->getDocument(); - $path = $this->documentInspector->getPath($event->getDocument()); - foreach ($this->documentInspector->getLocales($document) as $locale) { - $this->collectNavigationContexts($path, $locale); - } + $this->collectNavigationContexts($event->getDocument(), null); } public function collectNavigationContextBeforeRemovingLocale(RemoveLocaleEvent $event): void { - $path = $this->documentInspector->getPath($event->getDocument()); - $this->collectNavigationContexts($path, $event->getLocale()); + $this->collectNavigationContexts($event->getDocument(), $event->getLocale()); } - public function collectNavigationContexts(string $path, ?string $locale): void + private function collectNavigationContexts(object $document, ?string $eventLocale): void { + $path = $this->documentInspector->getPath($document); + $locales = $eventLocale ? [$eventLocale] : $this->documentInspector->getLocales($document); + $defaultNode = $this->defaultSession->getNode($path); $liveNode = $this->liveSession->getNode($path); - $propertyName = $locale ? - $this->propertyEncoder->localizedContentName('navContexts', $locale) : - $this->propertyEncoder->contentName('navContexts'); - $liveNavigationContexts = []; - $defaultNavigationContexts = []; - if ($liveNode->hasProperty($propertyName)) { - $liveNavigationContexts = $liveNode->getProperty($propertyName)->getValue(); + foreach ($locales as $locale) { + $propertyName = $this->propertyEncoder->localizedContentName('navContexts', $locale); + $liveNavigationContexts = []; + $defaultNavigationContexts = []; + + if ($liveNode->hasProperty($propertyName)) { + $liveNavigationContexts = $liveNode->getProperty($propertyName)->getValue(); + } + + if ($defaultNode->hasProperty($propertyName)) { + $defaultNavigationContexts = $defaultNode->getProperty($propertyName)->getValue(); + } + + $this->navigationContexts = \array_unique( + \array_merge( + $this->navigationContexts, + $liveNavigationContexts, + $defaultNavigationContexts + ) + ); } - if ($defaultNode->hasProperty($propertyName)) { - $defaultNavigationContexts = $defaultNode->getProperty($propertyName)->getValue(); - } - - $this->navigationContexts = \array_merge( - $this->navigationContexts, - $liveNavigationContexts, - $defaultNavigationContexts - ); } public function invalidateNavigationContexts(): void diff --git a/UPGRADE.md b/UPGRADE.md index bd37249..17b4590 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -1,5 +1,9 @@ # Upgrade +## 0.9.0 + +### Make NavigationInvalidationSubscriber::collectNavigationContexts method private + ## 0.6.0 ### Change constructor arguments of SingleSnippetSelectionResolver